This is a creation in Article, where the information may have evolved or changed.
Group Declaration
Import "FMT" import "OS" Const I = 100const II = 3.1415const III = "TEST" var i intvar II Float64var III string//block code below import ( "FMT" "OS") const ( i = 1 II = 3.1415 III = "Test") var ( i int II float64 III string)
Iota Enumeration
This keyword is used when declaring an enum, with a default starting value of 0 and +1 for each call.
Package Mainimport "FMT" const (x = Iotay = Iotaz = Iotaw) Const V = iota//Every encounter with the Const keyword, iota resets the func main () {FMT. Printf ("x=%d\ny=%d\nz=%d\nw=%d\nv=%d", X, Y, Z, W, v)}
A variable that starts with a capital letter is exportable, that is, the other package can read, is a common variable
non-exportable at the beginning of the lowercase letter, is a private variable
a function that starts with a capital letter is the same as a public function with a key word in the class
A function that starts with a lowercase letter is a private function with a private keyword
Array arrays
in [N]array, n means the length of the array, type of the storage element
var arr [N]array
Package Mainimport "FMT" Func Main () {var arr [5]int//declares a numeric value of type int arr[0] = 10arr[4] = 20fmt. Printf ("This first is%d\n", arr[0]) fmt. Printf ("This was%d\n", Arr[4])}
Description: Length is also part of the array type, so [5]int and [10]int are different types and arrays cannot change length.] If you use pointers, you need to use the slice type
Package Mainimport "FMT" Func Main () {a: = [5]int{1,2,3,4,5}//declares an int array of length 5 b: = [...] int{7,8,9}//can omit length, ' ... ' in a way that automatically calculates the length of the element according to the number of elements c: = [2][4]int{1,2,3,4},{5,6,7,9}//declares a two-dimensional array, two arrays as elements, There are 4 elements of type int in each array}
Slice
In the initial definition of an array, we do not know how large an array is needed, so we need a "dynamic array", a data structure called slice. Slice is not a real dynamic array, but a reference type. Slice always points to an underlying array
Declaration and array, just a short length var a []int
Slice by array[i:j], where I is the beginning of the array, J is the end, but does not contain array[j],
Declares an array containing 5 elements of type byte test: = [5]byte{' A ', ' B ', ' C ', ' d ', ' E '}//declares two Slicevar A with byte, b []bytea = Test[1:3]//test[1],tes T[2]b = Test[3:5]//test[3],test[4]
1, slice default start position 0,test[:n] equivalent to test[0:n]2, slice the second sequence is the length of the array, Test[n:] equivalent to test[n:len[test]]3, if you get slice directly, you can use test[: ]
Map
The map read and settings are similar to slice, which operates via key, except that the index of slice can only be of type int, and map has many types, which can be int,string and all the types that fully define = = and! =.
Package Mainimport "FMT" Func Main () {//declares a key to be a dictionary with a value of int, which requires the use of make to initialize//var numbers before use map[string]int// Another way to declare a map numbers: = Make (Map[string]int) numbers["test"] = 1numbers["test1"] = 2numbers["test2"] = 3fmt. Println ("Test2:", numbers["Test2"])}
The initialization of the map
Values can be initialized by Key:val, and the map has a built-in way to determine if a key exists
Package Mainimport "FMT" Func Main () {//Initialize a dictionary test: = map[string]float32{"A": 1, "B": 2, "C": 3}//map has two return values, second return value, if not present K EY returns flase, if there is a key returned Truetestarr, result: = test["a"]if result {fmt. Println ("Value:", Testarr)} else {fmt. PRINTLN ("Key does not Exist")}}
Delete a map element with delete
Delete (Testarr, "a")//delete element of key A
Map is a reference type
If two maps point to one level at a time, then one change and the other changes accordingly.
Package Mainimport "FMT" Func Main () {m: = Do (map[string]string) m["test"] = "TEST" m1: = mm1["Test"] = "change test"//Now, The value of m["test" has also been chage test the FMT. Printf ("changed test:%s", m["Test"])}
Make,new operation
Make
Make for built-in types (map, slice, channel) memory allocation make (T,args) has a different function than new (t), making can only create Slice,map,channel, and returns a T type with an initial value (not 0) instead of * T
New
New is used for various types of memory allocations "new return pointer" new (t) allocates a 0 value filled memory space of type T, and returns its address, a value of type *t (go language term: A pointer is returned that points to the 0 value of the newly assigned type T)
Summary: New is responsible for allocating memory, and new (T) returns *t pointer to a 0-value T Make is responsible for initializing the value, make (t) returns the initialized T, not the pointer
The most important point: make is only available for Slice,map and channel
With respect to "0 value", it is not a null value, but a default value of "before the variable is not populated", usually 0, as follows
int 0int8 0int32 0int64 0uint 0x0rune 0//rune The actual type is Int32byte 0x0//byte the actual type is Uint8float32 0//Length 4 Bytefloat64 0//Long Degree of 8 Bytebool falsestring ""