The first article from the public number: Go programming time

“Go programming time”, a can take you to learn the Go language column, at the same time welcome to search my namesake public account [Go programming time] (beautiful layout is more suitable for reading), the first time to get Go language dry materials.

A series of reading

1. Set up the Go language development environment

2. Five ways to create variables in Go language

3. Integer and floating point in Go

4. What are the differences between Byte, rune and string in Go?


1.7

An array of 1.

An array is a sequence of fixed-length elements of a particular type. An array can consist of zero or more elements. Because arrays are of fixed length, they are rarely used directly in Go.

Declare an array and assign a value to each element in the array (the minimum valid value for an index is 0, not 1, as in most other languages)

// the 3 in [3] represents the number of elements in the array
var arr [3]int
arr[0] = 1
arr[1] = 2
arr[2] = 3
Copy the code

Declares and initializes an array directly

// The first method
var arr [3]int = [3]int{1.2.3}

// The second method
arr := [3]int{1.2.3}
Copy the code

The 3 above represents the number of elements in the array. If you ever want to add an element to the array, you will have to change the number accordingly. To avoid this hard coding, you can write it like this, using… Let the Go language allocate space on its own based on the situation.

arr := [...]int{1.2.3}
Copy the code

[3]int and [4]int are arrays, but they are different types, using FMT %T.

import (
	"fmt"
)

func main(a) {
	arr01 := [...]int{1.2.3}
	arr02 := [...]int{1.2.3.4}
	fmt.Printf("The type of %d is: %T\n", arr01, arr01)
	fmt.Printf("The type of %d is: %T", arr02, arr02)
}
Copy the code

The output is as follows

[3]int [1 2 3 4]int [4Copy the code

If you feel like writing [3]int every time is a bit of a hassle, you can define a type literal for [3]int, which is an alias type.

You can use the type keyword to define a type literal. You can use this alias type later whenever you want to define an array of size 3 and type int.

import (
	"fmt"
)

func main(a) {
	type arr3 [3]int

	myarr := arr3{1.2.3}
	fmt.Printf("The type of %d is: %T", myarr, myarr)
}
Copy the code

The output is as follows

The [1 2 3] type is main.arr3Copy the code

2. Slice

A Slice, like an array, is a container that can hold several elements of the same type. Unlike arrays, there is no way to determine the length of a value by slice type. Each slice value has an array as its underlying data structure. We also refer to such arrays as the underlying arrays of slices.

A slice is a reference to a contiguous fragment of an array, so a slice is a reference type. This fragment can be the entire array or a subset of items identified by the start and end indexes. Note that items identified by the end indexes are not included in the slice.

import (
	"fmt"
)

func main(a) {
	myarr := [...]int{1.2.3}
	fmt.Printf("The type of %d is: %T", myarr[0:2], myarr[0:2])}Copy the code

The output is as follows

The type of [1 2] is: []intCopy the code

There are three ways to construct slices

  1. Fragment the array (as shown in the example above: myarr[0:2], 0 is the start index value, 2 is the end index value, and the interval is left and right)

  2. Ab initio declaration assignment (example below)

    // Declare a string slice
    var strList []string
    
    // Declare integer slices
    var numList []int
    
    // Declare an empty slice
    var numListEmpty = []int{}
    Copy the code
  3. Make ([]Type, size, cap) make([]Type, size, cap)

    This function specifies the three elements of a slice: Type, size, and cap.

    import (
    	"fmt"
    )
    
    func main(a) {
    	a := make([]int.2)
    	b := make([]int.2.10)
    	fmt.Println(a, b)
    	fmt.Println(len(a), len(b))
    	fmt.Println(cap(a), cap(b))
    }
    Copy the code

    The output is as follows

    [0 0] [0 0]
    2 2
    2 10
    Copy the code

The concepts of Len and cap may be confusing, but here’s an example:

  • The company name, which is literally the variable name.

  • All the stations in the company are equal to the allocated memory space

  • The employees in the company, the equivalent of elements.

  • Cap represents the maximum number of employees you can accommodate in your company

  • Len represents how many employees your company currently has

Since slice is a reference type, its zero value (the default) is nil if you do not assign to it

var myarr []int
fmt.Println(myarr == nil)
// true
Copy the code

Arrays are similar to slices in that they are containers that can hold several elements of the same type

The size of the array container is fixed, while the slice itself is a reference type. It’s more like a List in Python, and we can append it with elements.

import (
	"fmt"
)

func main(a) {
	myarr := []int{1}
	// Appends an element
	myarr = append(myarr, 2)
	// Append multiple elements
	myarr = append(myarr, 3.4)
	// Append a slice... Unpack, cannot be omitted
	myarr = append(myarr, []int{7.8}...).// Insert the element in the first position
	myarr = append([]int{0}, myarr...)
	// Insert a slice in the middle (two elements)
	myarr = append(myarr[:5].append([]int{5.6}, myarr[5:]...). ...). fmt.Println(myarr) }Copy the code

The output is as follows

[0 1 2 3 4 5 6 7 8]
Copy the code

Finally, I’ll leave you with a question to ponder. Here’s the code

import (
	"fmt"
)

func main(a) {
	var numbers4 = [...]int{1.2.3.4.5.6.7.8.9.10}
	myslice := numbers4[4:6:8]
	fmt.Printf("Myslice = %d, length: %d\n", myslice, len(myslice))

	myslice = myslice[:cap(myslice)]
	fmt.Printf("The fourth element of myslice is: %d", myslice[3])}Copy the code

Why is myslice of length 2 able to access the fourth element

Myslice is [5 6] and its length is: 2. The fourth element of myslice is: 8Copy the code