A Tour of Go Learning Summary

site

Packages

Go programs consist of packages. package mainIs executed from the main package The package name is the last element name in the import path

import

Import other programs and libraries

Exported names

In Go, names that start with a capital letter are exported names that can be referenced from external packages. Names that start with a lowercase letter cannot be referenced by external packages.

exportnames.go



package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.pi) //Cannot be referenced
	fmt.Println(math.Pi) //Can be referenced
}

Functions

syntax

A function can take zero or more arguments. Write the type after the argument name and the return type after the argument.

func function name(Argument 1 Type of argument 1,・ ・ ・)Return type{
processing
}

function.go



func add(x int, y int) int {
	return x + y
}

argument

If two or more arguments are of the same type, the last type can be left and omitted.

function.go



func add(x, y int) int {
	return x + y
}

Return value

The function can return multiple return values.

multiresult1.go



package main

import "fmt"

func swap(x, y string) (string, string) { //Return type()Defined in
	return y, x
}

func main() {
	a, b := swap("hello", "world")
	fmt.Println(a, b)
}

Named return values

You can give a name to the variable that will be the return value. If you give a name to the return value, it will be treated as the variable name defined at the beginning of the function. You can use a named return variable to return without writing anything in the return statement.

multiresult2.go



package main

import "fmt"

func split(sum int) (x, y int) {
	x = sum * 4 / 9
	y = sum - x
	return
}

func main() {
	fmt.Println(split(17))
}

Variables

syntax

Declare the variable with the var statement. Declare the type after the last variable. Initial values ​​can be defined for variables.

var1.go


package main

import "fmt"

var c, python, java bool
var k, j int = 1, 2

func main() {
	var i int
	fmt.Println(i, c, python, java)
	fmt.Println(k,j)
}

Omission method

In a function, you can make an implicit type declaration by using a short `` : introduced assignment statement instead of a var declaration. Outside the function, you need declarations that start with a keyword (var, func, etc.), and implicit declarations with ```: introduced` are not available. in

var2.go


package main

import "fmt"

//Var cannot be omitted outside the function
//n := 10

func main() {
	var i, j int = 1, 2
	k := 3
	c, python, java := true, false, "no!"

	fmt.Println(i, j, k, c, python, java)
}

Type definition

Built-in type

Type conversion

Type conversion in Go requires explicit conversion.

typeconversion.go


package main

import (
	"fmt"
	"math"
)

func main() {
	var x, y int = 3, 4
	var f float64 = math.Sqrt(float64(x*x + y*y))
	var z uint = uint(f)
	fmt.Println(x, y, z)
}

Type inference

If you declare a variable without specifying an explicit type (either `: introduced` or` var selected`), the type of the variable is type inferred from the variable on the right. If the variable on the right has a type, the new variable on the left will have the same type. If the number is untyped on the right, the new variable on the left will be of type int, float64, complex128 based on the precision of the constant on the right.

inference.go


package main

import "fmt"

func main() {
	i := 42
	fmt.Printf("i is of type %T\n", i)
	f := 3.14
	fmt.Printf("f is of type %T\n", f)
	c := 0.867 + 0.5i
	fmt.Printf("c is of type %T\n", c)
}

constant

Constants are declared like variables using the const keyword. Constants can only be used with characters, strings, booleans, and numbers. Constants cannot be declared using ```: introduced`.

const.go


package main

import "fmt"

const Pi = 3.14

func main() {
	const World = "world"
	fmt.Println("Hello", World)
	fmt.Println("Happy", Pi, "Day")

	const Truth = true
	fmt.Println("Go rules?", Truth)
}

Recommended Posts

A Tour of Go Learning Summary
A brief summary of Linux
A brief summary of Python collections
Machine learning ③ Summary of decision tree
A brief summary of qubits (beginners)
Summary of go json conversion behavior
A beginner's summary of Python machine learning is super concise.
Summary of snippets when developing with Go
A brief summary of Pinax overview #djangoja
Machine learning memo of a fledgling engineer Part 1
A memorandum of studying and implementing deep learning
Summary of evaluation functions used in machine learning
Machine learning memo of a fledgling engineer Part 2
Get a glimpse of machine learning in Python
Create a dataset of images to use for learning
Numerical summary of data
Machine learning tutorial summary
Summary of Tensorflow / Keras
Go language learning record
A miscellaneous summary of what I researched about Ansible
Machine learning ⑤ AdaBoost Summary
I made a vim learning game "PacVim" with Go
Summary of pyenv usage
Deep learning 1 Practice of deep learning
Installation of TensorFlow, a machine learning library from Google
A brief summary of Linux antivirus software for individuals
Basics of Python learning ~ What is a string literal? ~
Summary of string operations
Features of Go language
Summary of the basic flow of machine learning with Python
Summary of Python arguments
What is a recommend engine? Summary of the types
Summary for learning RAPIDS
Summary of logrotate software logrotate
Summary of test method
Image crawling summary performed at the speed of a second
A rough summary of the differences between Windows and Linux
[For beginners] A word summary of popular programming languages (2018 version)
Summary of Python3 list operations
A simple sample of pivot_table.
A memorandum of kernel compilation
2017.3.6 ~ 3.12 Summary of what we did
Basics of Machine Learning (Notes)
Linux a summary shortcut key
Reinforcement learning 2 Installation of chainerrl
Other applications of dictionary learning
A small memorandum of openpyxl
Convenient usage summary of Flask
Machine learning ② Naive Bayes Summary
Summary of Linux distribution types
go language learning miscellaneous notes 1
Machine learning article summary (self-authored)
Supervised learning 1 Basics of supervised learning (classification)
Build a machine learning environment
Importance of machine learning datasets
Basic usage of Pandas Summary
Machine learning ④ K-nearest neighbor Summary
1st month of programming learning
Deep reinforcement learning 2 Implementation of reinforcement learning
Summary of Proxy connection settings
[Learning memo] Django command summary