Akik Look at that coder

Public account: Look at that code farmer

Last installment introduced the Array of Go language learning | Go Theme month

  • Definition of an array
  • Array declaration
  • Initializing an array
  • Iterate over a set of elements
  • A classic case of arrays

This article will continue to take you into the world of Go.

1. Introduction to this paper

Use slices of Go language learning

2. Use of slices

Arrays are an important artifact in Go, but slicing is used more often.

The internal structure of the slice contains the address, size, and capacity.

Slicing is generally used to quickly manipulate a collection of data. When it comes to combining data into a cake, slicing is what users want.

The cutting process includes where to start (i.e. the address of the slice) and how big to cut (i.e. the size of the slice). The capacity can be understood as the size of the pocket that holds the slice.

As shown below:

3. Why slice?

In Go language, the use of array has certain limitations, cannot directly add elements to the array, slice is more flexible than array, users can quickly add and delete elements in slice.

4. Declare slices

Each type can have its slice type, representing a contiguous collection of elements of multiple types. The declaration format of the slice type is as follows:

var name []T

  • Name indicates the variable name of the slice type
  • T represents the element type corresponding to the slice type.

5. Use the make function to construct slices

If you need to create a slice dynamically, you can use the make built-in function in the following format:

make([]T, size, cap)

  • T: the element type of the slice
  • Size: Is how many elements are assigned to this type
  • Cap: number of pre-allocated elements. This value does not affect size, but can allocate space in advance to reduce performance problems caused by multiple space allocations.

As shown in the following case:

package main

import (
   "fmt"
)

func main(){
   a := make([]int,2)
   b := make([]int,2.10)
   fmt.Printf("Length of slice A: %v, length of slice B: %v",len(a),len(b))
}
Copy the code

The output is:

A and B are pre-allocated slices of 2 elements. However, 10 elements have been allocated to the internal storage space of B, but 2 elements are actually used. The capacity does not affect the current number of elements, so the length of both a and B is 2.

6. Generate a new slice from an array or slice

By default, a slice points to a contiguous memory region, either an array or the slice itself.

Common slicing formats are as follows:

Slice [start position: end position]

  • Slice: indicates the target slice object
  • The start position represents the index of the corresponding target slice object.
  • The end position indicates the end index of the corresponding target slice.

As shown in the following case:

package main

import (
   "fmt"
)

func main(){
   var a = [3]int{1.2.3}
   fmt.Printf("Array: %v, slice after processing: %v",a,a[1:2])}Copy the code

The output is

7. Represents the original slice

In the format of generating slices, when both the start and end positions are empty

The generated section will represent the section consistent with the original section, and the generated section is consistent with the original section in data content.

The specific code is as follows:

package main

import (
   "fmt"
)

func main(){
   a := []int{1.2.3}
   fmt.Printf("Original slice: %v, generated slice: %v",a,a[:])
}
Copy the code

The output is

8. Reset the slice to clear out the owned elements

When both the start and end positions of the slice are set to 0, the resulting slice becomes empty

The specific code is as follows:

package main

import (
   "fmt"
)

func main(){
   a := []int{1.2.3}
   fmt.Printf("Original slice: %v, empty slice: %v",a,a[0:0])}Copy the code

The output is

9. Add elements to slices

The Go language uses the built-in function Append () to dynamically add elements to slices.

The specific code is as follows:

package main

import "fmt"

func main(){
   var str = make([]string,2)
   str[0]  = "First"
   str[1]  = "Second"
   fmt.Printf("Original slice: %v\n",str)
   str =append(str,"Third")
   fmt.Printf("The slice after adding elements is: %v",str)
}
Copy the code

The output is:

10. Delete elements in the slice

Deleting elements from slices can also be done using the built-in append() function.

The essence of deleting an element is to delete an element as a cut-off point, reconnecting the memory of the two parts before and after.

The code removal process can be represented as follows:

The specific code is as follows:

package main

import "fmt"

func main(){
   str := []string{"a"."b"."c"."d"."e"}
   index: =2
   fmt.Printf(\n: %v \n: %v \n: %v \n: %v \n: %v \n: %v \n,str[:index],str[index],str[index+1:])
   str=append(str[:index],str[index+1:]...). fmt.Printf("The slice after deleting elements is: %v",str)
}
Copy the code

The output is:

11. Copy the elements in the slice

To copy all or part of the elements in a slice, use the built-in function copy().

Before copying an element in a slice, you must declare another slice of the same type as the slice

The specific code is as follows:

package main

import "fmt"

func main(){
   var str=make([]string,2)
   var copystr=make([]string,2)
   str[0] ="a"
   str[1] ="b"
   copy(copystr,str)
   fmt.Println(copystr)
}
Copy the code

The output is:

If you find this helpful:

1. Click “like” to support it, so that more people can see this article

2, pay attention to the public number: look at that code farmers, we study together and progress together.