This is a creation in Article, where the information may have evolved or changed. 5th chapter, array, slice, dictionary, the 33-page group is the set of elements that are stored sequentially in the same length type. var x [5]int
Func main () {var x [5]int X[4] = the FMT. PRINTLN (x)} array subscript starts at 0. Above code output: [0 0 0 0 100]
var x [5]float64
X[0] = 98
X[1] = 93
X[2] = 77
X[3] = 82
X[4] = 83
var total float64 = 0
For I: = 0; i<5; i++ {
Total + = X[i]
}
Fmt. Println (TOTAL/5)
The above code: calculates the average of five numbers in an array.
var x [5]float64
X[0] = 98
X[1] = 93
X[2] = 77
X[3] = 82
X[4] = 83
var total float64 = 0
For i:=0; I<len (x); i++ {
Total + = X[i]
}
Fmt. Println (reflect. TypeOf (len (x)))
The Len () method returns the int type value.
Fmt. Println (Total/len (x))
The above code will report:. \floatcal.go:26:invalid Operation:total/len (x) (mismatched types float64 and int) means that the float type is divided by the int type. reflect. The TypeOf method can return the type of a variable or expression.
to import "reflect" Import (
"FMT"
"Reflect"
)
FMT. Println (Total/float64 (len (x)))
Force the type to convert a bit.
var total float64 = 0
For _, Value: = Range x {
Total + = value
}
Fmt. Println (Total/float64 (len (x)))
Golang some of the class library is not used or can not, and will error. _ You can loop an array without iterators, or variable i, without using the loop counter.
Initialize array notation: x: = [5]float64{98, 93, 77, 82, 83} or x: = [5]float64{98, 93, 83, 77, 82,}
a := [ 2 ][ 2 ] int { { 1 , 2 }, { 3 , 4 } }
Slice slicesLike an array, you can change the length. A slice is a reference method. Make to initialize, the internal pointer initialization can be done, and then immediately use the
Unlike new, make returns a reference to a type instead of a pointer, and the return value depends on the type that is passed in
In the go language, there are 4 types of reference: Slice,map,channel,interface.
Slice,map,channel is typically initialized with make:
CI: = make (chan int)//unbuffered channel of integers no buffer integer channel
CJ: = Make (chan int, 0)//unbuffered channel of integers
CS: = Make (chan *os. File, +)//buffered channel of pointers to Files
func make (t Type , size Integertype) Type
The second parameter is the initialization of the slice, the array length. The third parameter is the length of the reservation.
var x [5]float64
X[0] = 98
X[1] = 93
X[2] = 77
X[3] = 82
X[4] = 83
Y: = make ([]float64, 5)
Z: = Make ([]float64, 2, 3)
Z1: = Append (z, 1, 2, 3, 4)
Fmt. Println (len (x))
Fmt. PRINTLN (x)
Fmt. Println (Len (z))
Fmt. Println (z)
Fmt. Println (Z1)
But I added 4 more elements to go in. Who is the 3 reserved for? By document, x: = make ([]float64, 5, 10) means X has a total of 10 element positions. 5 are used or initialized. There are also 5 locations available.
Another way to create slices is to create them through an array. Arr: = [5]float64{1, 2, 3, 4, 5}x: = Arr[1:4]
Arr[1:4] means: [2, 3, 4], note [Low, high] the high does not contain themselves namely low >= 1, high < 4
Append (arr1, arr2) Append (arr1, 1, 2, 3, 4) two append array
x: = []float64{1, 2, 3, 4, 5}
Y: = make ([]float64, 2)
Fmt. Println (y)
Copy (y, x)
Copy (x, y)
Fmt. Println (x, y)
Output: [0 0]
[0 0 3 4 5] [0 0]
Y, copy to x [0 0]
[1 2 3 4 5] [1 2]
Dictionary MapPage 38 is a collection of unordered key-value pairs (also known as maps, associative arrays, hash tables, or dictionaries). network resources article: http://studygolang.com/articles/2494 dictionary implements the basic function of hash table (hash) Elements of the query, add and remove go is a strongly typed language, you must specify the type of the map key and value of Var x map[string ]int
m: = Make (map[string]string)
m["bandname"] = "Funny Bones"//"create" websitetitle: = m["bandname"] + "Music"//"read" m["bandname"] = "m Oon Taxi "//" Update "Delete (M," bandname ")//" Delete "FMT. Printf (m["Bandname"])//prints Nothing since m["bandname"] = = ""
Traverse
For key, Value: = range m {fmt. Println ("Key:", Key, "Value:", Value)}
Extended read: http://blog.csdn.net/abv123456789/article/category/2206183