1. Variables
var < name > type
2. Initialization
var v1 int = 10
V3: = ten//v3 cannot be a variable that has already been declared
3. Ignore return values
_, _, Nick = GetName ()
4. Constants & Iota
Const Pi float64 = 3.1415
Const (
Size Int64 = 1024
EOF =-1
)
Const A, b, C = 3, 5, "XX"
Const (
V1 = 1 << Iota
V2
V3
)//Iota increment starting from 0 before the next const
5. Os. GETENV ("HOME")
6. Start with uppercase, visible outside the package
7. UIntPtr
Floating point comparison--using poor precision
8. plural
v = Complex (3.1, 3.2)
Real (v) = = 3.1
Imag (v) = = 3.2
9. String, cannot be assigned as [x]
10. String type
BYTE---uint8
Rune----Unicode Characters
11. Array declarations
var array_ex [32]byte = [32]byte {1, 2, ...}
Len ()
For the value type, in the fun internal modification, does not affect the fun external value
12. Array slicing
var MyS []int = Array_ex[:5]
var MYSL = make ([]int, 5)
Mysli: = []int{1, 2, 3}
Len/cap/append.
Len (slice) = number of elements
Cap (slice) = memory already allocated
. Append ()
MyS = Append (MyS, 1, 2, 3)
MyS2: = []int{8, 9, 10}
myS3 = Append (MyS, myS2 ...) Break myS2, join with multiple elements, append parameter requirements
14.copy ()
Copy (s1, S2)//Smaller to determine the size of the copy
15.
Map
var persondb map[string] PersonInfo
Persondb = Make (map[string] PersonInfo)
persondb["123"] = personinfo{"123", "Tom", "203"}
person, OK: = persondb["123"]
If OK XXX
MyMap = Make (map[string] PersonInfo, 100)
Delete (MyMap, "123")
16.
If return else return
Need a final return
Fallthrough.
Switch X {
Case 0:
....
Fallthrough//Continuation case
Default
}
. for {infinite loop
No while statement
. Goto
20. Functions
Func <func-name> (var ...) (ret ...) { ...
21. Indeterminate Parameters
Func myFunc (args ... int) {
For _, arg: = Range args {
}
}
Call
Myfun ([] int {1, 2, 3, 5})
Parameter passing:
Fun A (args ... int) {
B (args ...)
C (args[1:] ...)
}
22. Any type
Func Printf (format string, args ... interface{}) {
Func myprint (Args ... interface {}) {
For _, arg: = Range args {
Switch arg. (type) {
Case INT:
Case string:
Default
}
}
}
23. Multiple Return values
Carry Error
_: Ignore return value
24. Anonymous functions
F: = Func (A, b int, z float64) bool {
}
Func (ch Chan int) {
CH <-ACK
} (Reply_chan)
25. Closures
Func Main () {
var J int = 5
A: = Func () (func ()) {//parameter is NULL, return value is a function
var i int = 10
Return Fun () {
} ()
}
A ()
J *= 2
A ()
errno.
Defer fun () {//do complex cleanup work
It will be called no matter what.
Panic () Recover ()
Panic () immediately terminates, returns the call, defer is called
Recover () Handling panic exceptions
Defer func () {
If r: = Recover (); R! = Nil {
Log. Printf ("Return:%v", R)
} ( )
}
Flag Package: Command-line argument parsing
X = flag. String ("..... ")
Flag. Parse ()//called in Main, parameter resolved to value in X
File, err: = OS. Open ()
Defer file. Close ()
values = Make ([]int, 0)
for {
Line, Isprefix, err1: = Br. ReadLine ()
Err1! = Nil; Err1! = Io. Eof
}
str: = string (line)
Value, err1: = StrConv. Atoi (str)
If err1! = nil ...
Values = Append (values, value)
writevalues func (values [] int, outfile string)
File, err: = OS. Create (outfile)
str: = StrConv. Itoa (value)
File, err: = OS. Open (infile)
Defer file. Close ()
BR: = Bufio. Newreader (file)
Line, Isprefix, err1: = Br. ReadLine ()
Err1! = Nil, err1! = Io. Eof
T1: = time. Now ()
[Learn] GO language learning notes (i)