Go Language Learning Notes

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go language Learning
Basic type
Bool Value Range: True,false (cannot be replaced by numbers)
Int/uint may be 32 or 64 bits depending on the platform
Int8/uint8 Length: 1 byte value range -128~127/0~255
Byte (uint8 alias)
Int16/uint16 Int32/uint32 Int62/uint64
Float32/float64 length: 4/8 byte decimal place: Accurate to 7/15 decimal digits
Plural: complex64/complex128 Length: 8/16 bytes sufficient to hold the pointer to a 32-bit or 64-bit integer type
0 value is the default value of the variable


In the submit ctrl+b is open our test window, go Run is the running program

Only Transitions <valueA> [:]=<typeofvaluea> (<ValueB>) can be displayed in the Go language
Floating-point and Boolean types cannot be converted
The string is converted to the int type var Valuea, _ = StrConv. Atoi (VALUEB)
The int type is converted to the string type valuea:= StrConv. Itoa (VALUEB)


There are pointers in go
Adopt the "." Selectors to manipulate the members of the pointer target object
Operator "&" takes the variable address and uses "*" to indirectly access the target object using the pointer
The default value is nil, not null

The curly braces in an If statement must be followed by a conditional statement, which can have no parentheses, and you can declare a variable in a conditional statement

For statement for{} Infinite loop for i:=0;i<3;i++ {} Classic mode

Switch supports conditional statements of any type or expression, does not require a break, matches the condition automatically, and needs to continue with the next case, requires the use of the Fallthrough statement, supports the initialization expression, the right side with the semicolon, and the brace must be on the same line as the conditional statement.

Goto break and continue will be used in conjunction with the tag, otherwise the break will only jump out of the loop


Array var a [2]int//array of type int with length 2
The default value is zero if no value is assigned, and string defaults to NULL
A: =[2]int{1}//Array The first number is 1, the second is the default 0
A: =[20]int{19:1}//Add an index, 20th element is 0, others 0
A: =[...] int{1,2,3,4,5}//array of length 5
A: =[...] INT{19:1}//Index 20 element is 1, other is zero, array length is 20

Array pointer A: = [...] Int{19:1}
The address of Var P *[100]int= &a//p bit array A
Pointer array x,y=1,2
A:=[...] Int{&x,&y}//a outputs the address of X, y
Comparisons between arrays can be made = = and! =

Sliced slice
itself is not an array, it points to the underlying array
You can create directly or get a build from the underlying array
Len () Gets the number of elements, the cap () gets the capacity, make () is created, and if multiple slice point to the same underlying array, one of the value changes affects all
Make ([]t,len,cap) if the CAP is omitted, then the value of Len is the same

var a=[]int//Slice definition

A:=[10]int
S1:=A[5:]//Slice s1 take array a index 5 to the last element
S1:=a[:5]//s1 the elements of array a index 0 to 5
S1:=a[3:9]//s1 The elements of the array a index 3 to 9

S1: =make ([]int,3,6)//define S1 slices, length 3, capacity 6
S1=append (s1,1,2,3)//Use the Append function to append elements to the slice S1 1.2.3


Map
var m map[int]string//map definition
M=make (map[intstring])//initialization
m[1]= "OK"//Assignment
A: =m[1]//Take value
Delete (m,1)//Delete value

Multiple map nesting requires a second layer of initialization
var m map[int]map[int]string
M=make (map[int]map[int]string)
M[1]=make (map[int]string)//initialization for key 1
M[1][1]= "OK"
A:=m[]

Range Iteration function
Sort. Ints (s) sort

Indirect ordering of maps
M: =map[int]string{1: "A", 2: "B", 3: "C", 4: "D", 5: "E"}//Build Map
S:=make ([]int,len (M))//built Slice
I:=0
For K,_:=range m{//iteration take the value of each key and save it in slice
S[i]=k
i++
}
Sort.ints (s)//Sort Slice


The Go function does not support nesting, overloading, and default parameters
No need to declare prototypes, variable length arguments, multiple return values, named return value parameters
Anonymous functions, closures
Defer
After the function body execution finishes
Execute individually in reverse order of call order
Even if the function has a critical error, it will execute
Calls that support anonymous functions
Often used for resource cleanup, file closing, unlocking, and recording time
You can modify the function calculation result after return by cooperating with the anonymous function
If a variable in the function body is an argument to an anonymous function when defer, the defer is defined
The copy is already obtained, otherwise it is the address of the reference to a variable
Go has no exception mechanism, but has panic/recover mode to handle errors
Panic can be thrown anywhere, but recover is only valid in functions called by defer

Structural body
The struct in Go is very similar to the struct in C, and go has no class
Use Type <Name> struct{} to define structure, name follows visibility rules
Supports pointer-type members pointing to themselves
Supports anonymous structures that can be used as members or define member variables
Anonymous structures can also be used for map values
Structure can be initialized with literal values
Allows you to read and write struct members directly through pointers
Members of the same type can perform direct copy assignment
The = = and! = Comparison operators are supported, but not > or <
Support for anonymous fields, essentially a field defined by a type name
Embedded structure as anonymous field looks like inheritance, but not inherited
You can use the anonymous field pointer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.