Recent work some code is written in the go language and has learned some basic grammar of the Go language. Later there are some other learning summaries that are updated in this document.
1 Syntax 1.1 variable/constant definition
var v Type
var v Type = value
var v = value
var v1,v2,v3 Type
var v1,v2,v3 Type = Value1,value2,value3
var v1,v2,v3= value1,value2,value3
v1,v2,v3:= Value1,value2,value3
I,j = J,i
: = for a short declaration, the compiler automatically infers the appropriate type based on the initialized value, but it cannot be defined outside the function.
Const
s = "string"
PI = 3.1415
Pi2
)
var
I int
Pi float32 = 3.1415
)
1.2 Array & Slice
Array definition
var a [2]int = [2]int{1,2}
A: = [2]int{1,2}
A: = [...] int{1,2,3,4}
A2: = [2][3]int{[3]int{1,2,3},[3]int{3,2,1}}
A2: = [2][3]int{{1,2,3},{3,2,1}}
Slice definition
Slice is a reference type
var s []int = make ([]int,n)
s: = []int{v1,v2,v3}
1.3 Map
var m map[keytype]valuetype//keytype can be a string and fully define the type of ==/!= operation
M: = Make (Map[keytype]valuetype)
M:=MAP[KEYTYPE]VALUETYPE{K1:V1,K2:V2,K3:V3}
Len (M map[keytype]valuetype) int
Delete (M map[type]type1,key Type)
1.4 If statement
A) The conditional statement does not need to enclose the condition with parentheses;
b) No matter how many statements the statement body has, the curly braces {} must exist;
c) The left curly brace {must be on the same line as if or else;
D) After the IF, the conditional statement can be added before the variable initialization statement, with;
e) In a function that has a return value, the "final" return statement is not allowed to be included in the If...else ... , otherwise a compilation error will occur.
1.5 Go statement
Go FMT. Println ("go!") go statements consist only of a keyword go and an expression statement.
The execution of the GO statement is not necessarily associated with the execution of the expression statement it carries. The only certainty here is that the latter will happen after the former is complete. When the GO statement is executed, the function it carries (also known as the Go function) and the parameters to be passed to it (if any) are encapsulated into an entity (i.e., goroutine) and placed into the corresponding queue to be run. The run-time system of the go language will remove the goroutine from the queue and perform the corresponding function call operation in a timely manner. Note that the evaluation of those parameters passed to the function here is performed when the GO statement is executed. This is similar to the defer statement.
One word is that the GO statement is a thread.
1.6 SELECT statement
Select is a control structure in go, similar to the switch statement used for communication. Each case must be a communication operation, either sent or received. Select executes a running case randomly. If there is no case to run, it will block until there is one to run. A default clause should always be operational.
The syntax for the SELECT statement is described below:
(1) Each case must be a communication
(2) All channel expressions will be evaluated
(3) All expressions that are sent will be evaluated
(4) If any one of the communication can be carried out, it executes; others are ignored.
(5) If more than one case can be run, select randomly and fairly chooses an execution. Others will not be executed.
Otherwise:
If there is a default clause, the statement is executed.
If there is no default sentence, select blocks until a communication can be run, and go does not re-evaluate the channel or value.
1.7 Nil explanation
(1) Nil is not one of the key words of go, nil means none, or 0 value. In the go language, if you declare a variable but do not assign it, the variable will have a default value of 0 for the type. This is the corresponding 0 value for each type:
Nil pointers
Nil slices
Nil, maps
Nil channels
Nil functions
Nil interfaces
(2) For the method of pointer object, even if the value of the pointer is nil also can be called;
(3) A nil slice, in addition to not indexed, other operations are possible, when you need to fill the value of the APPEND function can be used, slice will automatically expand.
Nil slices
var s []slice
Len (s)//0
Cap (s)//0
For range S//iterates zero times
S[i]//Panic:index out of range
(4) For nil map, we can simply think of it as a read-only map, can not write operations, otherwise it will be panic.
Nil maps
var m map[t]u
Len (m)//0
For range m//iterates zero times
V, OK: = M[i]//Zero (U), False
M[i] = x//panic:assignment to entry in nil map
(5) Closing a nil channel will cause the program to panic. +
(6) interface is not a pointer, its underlying implementation consists of two parts, a type, a value, which is similar to: (Type, value). Only nil is equal if the type and value are nil.
1.8 Pipe (channel)
Pipelines are communication between goroutine that the go language provides at the language level, and we can use the channel to pass messages between multiple goroutine. Channel is the process of communication, is not support cross-process communication, if the need for interprocess communication, you can use the socket and other network methods.
A pipeline is type-dependent, that is, a pipe can pass only one type of value. The data in the pipeline is FIFO.
Grammar:
1//Declaration method, in this elemtype refers to the type that this pipeline passes
2 var channame chan Elemtype
3//Declare a pipeline that passes type int
4 var ch Chan int
5//Declare a map with the element bool type Channel
6 var m map[string] Chan bool
8//Define syntax, define the need to use built-in function make (), the following line of code is declaration + define an integral pipe
9 ch: = make (chan int)
10//Define the size of the pipe beforehand, this line of code defines the size of the pipe as 100
One ch: = make (chan int, 100)
13//Read and write data in the pipeline, the <-operator is preferred with the left-most Chan
14//write a data to the pipeline where it is important to note that writing data to a pipeline usually causes the program to block until a
15//Other goroutine reading data from this pipeline
ch<-value
17//Read data, note: If there is no data in the pipeline, reading the data from the pipe causes the program to block until there is data
* Value: = <-ch
20//Unidirectional piping
The Var ch1 chan<-float64//can only write float64 data to the inside, cannot read
$ var CH2 <-chan int//can only read int type data
24//Close channel, call Close () directly
Close (CH)
26//Determine if CH is off, determine the value of OK, if false, it is closed (the read is not blocked if it is closed)
X, OK: = <-ch
Examples of usage:
1 package Main
2 Import "FMT"
3
4 func print (ch chan int) {
5 FMT. Println ("Hello World")
6 ch<-1
7}
9 Func Main () {
Ten CHS: = make ([]chan int)
One for I: = 0; I < 10; i++ {
Chs[i] = make (chan int)
Go print (Chs[i])
14}
For _, ch: = Range (CHS) {
<-ch
18}
19}
Go Language Learning Summary