Go language notes
Beginner notes, please forgive me for any mistakes, and will be enriched and updated with the deepening of learning
Features
Concise, fast, safe
Parallel, interesting, open source
Memory management, array security, fast compilation
Main features
Automatic garbage collection
Richer built-in types
Function multiple return values
Error handling
Anonymous functions and closures
Types and interfaces
Concurrent programming
Reflection
Language interactivity
Language usage
A system programming language used in giant central servers equipped with Web servers, storage clusters or similar purposes.
Provides massive parallel support and is suitable for game server development
execute program
Basic program composition
package main //包声明
import "fmt" //引入包
func main() {
//函数
// 注释 /* 第一个简单的程序 */
fmt.Println("Hello, World!")
//变量
//语句 & 表达式
}
Need to pay attention to'{' cannot be a single line
There are two ways to execute the program
- go run filename.go
- go build filename.go generates binary file filename
./filename
About the bag
The file name is not directly related to the package name, and the file name and package name do not have to be the same.
The folder name is not directly related to the package name and does not need to be consistent.
The files in the same folder can only have one package name, otherwise the compilation error will be reported.
type of data
Boolean type
true or false:
var b bool = true;
Number type
Integer type int and floating point type float32, float64
String type
Go string is a string of
Go language connected by a single byte. UTF-8 encoding is used to identify Unicode text.
Derived type
- Pointer type (Pointer)
- Array type
- Structured type (struct)
- Channel type
- Function type
- Slice type
- Interface type (interface)
- Map type
grammar
Declare variable
Variable names in Go language consist of letters, numbers, and underscores. The first character cannot be a number.
The general form of declaring variables is to use the var keyword:
was identifier type
Simple example
var a int //If it is not initialized, it will be zero value, bool zero value is false, string is an empty string
var b int = 32
var c, d int //declare multiple variables
var str1 string = “str”
e := 10 / /If var is omitted, the variable must be newly declared and can only appear in the function body
Multivariate declaration
Multiple variables of the same type, non-global variables
var vname1, vname2, vname3 type
vname1, vname2, vname3 = v1, v2, v3
Much like python, no need to display the declared type, automatic inference
var vname1, vname2, vname3 = v1, v2, v3
Variables that appear on the left side of := should not have been declared, otherwise it will cause compilation errors
vname1, vname2, vname3: = v1, v2, v3
This way of factoring keywords is generally used to declare global variables
var (
vname1 v_type1
vname2 v_type2
)
Value type and reference type
All basic types like int, float, bool, and string are value types. Variables using these types directly point to the value stored in memory.
Use the equal sign = to assign the value of one variable to another variable, such as: j = i In fact, the value of i is copied in the memory
(it can be understood as a deep copy)
A reference type variable r1 stores the memory address (number) where the value of r1 is located, or the location of the first word in the
memory address. This memory address is called a pointer, and this pointer is actually stored in another one Value in
When the assignment statement r2 = r1 is used, only the reference (address) is copied.
If the value of r1 is changed, then all references to this value will point to the modified content
(can be understood as a shallow copy)
You can exchange the values of two variables directly like python:
a, b = b, a
Blank identifier
The blank identifier _ is also used to discard values. For example, the value 5 is discarded in: _, b = 5, 7.
_ Is actually a write-only variable, and its value cannot be obtained. This is because all declared variables must be used in the Go language, but sometimes it is not necessary to use all the return values obtained from a function.
such as:
package main
import "fmt"
func main() {
_,numb,strs := numbers() //只获取函数返回值的后两个
fmt.Println(numb,strs)
}
//一个可以返回多个值的函数
func numbers()(int,int,string){
a , b , c := 1 , 2 , "str"
return a,b,c
}
The final output is 2 str
Parallel assignment is also used when a function returns multiple return values. For example, val and error err are obtained by calling Func1 at the same time: val, err = Func1(var1).
constant
Constant definition
The definition format of constants:
const identifier [type] = value
[Type] can be omitted, the compiler will automatically infer the
explicit type definition: const b string = "abc" the
implicit type definition: const b = "abc"
iota
iota, a special constant, can be considered a constant that can be modified by the compiler.
iota will be reset to 0 when the const keyword appears (before the first line inside const), and every new line of constant declaration in const will count iota (iota can be understood as the line index in the const statement block).
const (
a = iota
b = iota
c = iota
)
The first iota is equal to 0. Whenever iota is used in a new line, its value is automatically increased by 1; so a=0, b=1, c=2 can be abbreviated as follows:
const (
a = iota
b
c
)
Examples:
package main
import "fmt"
func main() {
const (
a = iota //0
b //1
c //2
d = "ha" //独立值,iota += 1
e //"ha" iota += 1
f = 100 //iota +=1
g //100 iota +=1
h = iota //7,恢复计数
i //8
)
fmt.Println(a,b,c,d,e,f,g,h,i)
// 0 1 2 ha ha 100 100 7 8
}
Example of shift left:
package main
import "fmt"
const (
i=1<<iota
j=3<<iota
k
l
)
func main() {
fmt.Println(i,j,k,l)
// 1 6 12 24
}
iota means to automatically add 1 from 0, so i=1<<0, j=3<<1 (<< means left shifting), that is: i=1, j=6, this is no problem, the key lies in k And l, k=3<<2, l=3<<3 from the output result.
Simply stated:
i=1: shift to the left by 0 bits, and remain unchanged as 1;
j=3: shift to the left by 1 bit, and become binary 110, which is 6;
k=3: shift by 2 bits to the left, and become binary 1100, which is 12;
l =3: Shift left by 3 bits, it becomes 11000 in binary, that is, 24.
注:<<n==*(2^n)。
Operator
Similar to C++ (maybe all languages are similar):
Bitwise operator
Assignment operator
Other operators
Conditional statements
In addition to if, if...else, if nesting and switch, there is also select
select statement is similar to switch statement, but select will execute a runnable case randomly. If there is no case to run, it will block until there is a case to run.
Go has no ternary operator
loop statement
for, for nested
control: break, continue, goto