This is the 8th day of my participation in Gwen Challenge

These reviews

Previous articles focused on the branching and looping control structure of Go based syntax. The keywords, such as If, Switch and for, are commonly used in our daily coding. When we operate a large number of variables of the same type in the program, we need to use the power of containers to facilitate data storage and manipulation. In this article we introduce Go containers: arrays.

Containers commonly used in Go

Golang provides common container implementations in the form of a standard library, which basically meets our daily development needs. Let’s look at the use of the Go array.

An array of

The Go language provides array-type data structures.

An array is a sequence of numbered, fixed-length data items of the same unique type, which can be any primitive type such as integer, string, or custom type.

Relative to the statement number0, number1… Numbers [0], numbers[1]… Numbers [99] is more convenient and easy to expand.

Array elements can be read (or modified) by indexes (positions), starting at 0, with the first element indexed at 0, the second at 1, and so on. The array declaration style looks like this:

var name [size]T
Copy the code

The array size must be specified, either as a constant or as an expression, but must be determined statically at compile time, not dynamically. T represents the type of the array member, which can be any type.

Arrays can be initialized and used in the same way as in other languages. Arrays can be initialized using an initializer list at declaration time, or data members can be accessed and assigned by subscript, as shown below:

func main(a)  {
	var classMates1 [3]string
	classMates1[0] = "Xiao Ming"
	classMates1[1] = "Little red"
	classMates1[2] = "Xiao li" // Assign values to group members by subscript
	fmt.Println(classMates1)
	fmt.Println("The No.1 student is " + classMates1[0]) // Access array members by subscript

	classMates2  := [...]string{"Xiao Ming"."Little red"."Xiao li"} // Initialize the list with the initializer list
	fmt.Println(classMates2)

}
Copy the code

When initializing an array with an initializer, note that the size of the array in [] must be the same as the number of members in {}. In this example, we use… Let the compiler size the array for us based on the number of members in {}.

We can also manipulate arrays with Pointers, as in the following example:

classMates3 := new([3]string)
classMates3[0] = "Xiao Ming"
classMates3[1] = "Little red"
classMates3[2] = "Xiao li"
fmt.Println(*classMates3)
Copy the code

We use the new function to request the memory space of [3]string and return the corresponding pointer. Note that this pointer does not support offsets and operations, which is Golang’s limitation on pointer types. We can manipulate arrays directly with Pointers, just like Pointers in C.

summary

An array is a contiguous segment of memory that stores a fixed type and a fixed length. Its size is fixed when it is declared. Although the size of an array is immutable, its members can be modified.

There are also slicing containers similar to arrays in Go language. For the use of slicing, many people do not know the difference between slicing and array. Our next article will introduce the use of slicing.