Package Mainimport "FMT" Func Main () {//array var a = [3]int{}//equivalent to [3]int{0,0,0}a[0] = 1changeArray (a) fmt. Println (a) b: = [...] The int{1,2,3}//ellipsis notation allows the compiler to automatically calculate the length of an array based on subsequent initialization, but this length is the FMT determined at compile time. Println (b) C: = new ([3]int)//new returns an address, but can be accessed as Pointname[index], but the output needs to fetch the address corresponding to the value c[1] = 17//here and (*C) [1] equivalent, but not like C operation, unless using the unsafe module FMT. Println (*c)///////////////////////////////////////////////////////////////slice D: = []int{0,0,0}//similar to array but not specified length d[0] = 1fmt. Println (d) E: = make ([]int,5,10)//make can only create Slice,map,chanel and return a reference, new can create any type, return address e[1] = 12fmt. Println (e) changeslice (e) fmt. PRINTLN (e) F: = *new ([]int)//New can create any type and return an address, but cannot specify size with parameters, this method is very infrequently used F = append (f,1,2,3) fmt. Println (f) G: = c[:]//Create a slice based on an array, this method is very simple and practical [:] means that all elements can also be used [M:] [: n] [m:n]//Once a slice operation of an array returns the Slice FMT. Println (g) The}//array is a value reference, so the modification of the function array does not affect the original value and [3]int and [4]int is different type//if the value of the array must be changed, the requirement explicitly specifies a pointer to the passed array///For example here with Changearray (x *[3] int), called with Changearray (&a) func changearray (x [3]int) {x[1] = 100}//Slice is a reference, so the function changes the slice by affecting the original value func changeslice (x []int) { X[1] = 100}
Go language Learning-arrays and slices