Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of arrays and slices in Go language

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the introduction of arrays and slices in Go language. The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the introduction of arrays and slices in Go language.

1. Array

An array is a sequence of fixed-length elements of a particular type, and an array can consist of zero or more elements. Because arrays are fixed in length, arrays are rarely used directly in the go language.

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

The 3 in / / [3] indicates the number of elements in the array vararr [3] int arr [0] = 1 arr [1] = 2 arr [2] = 3

Declare and directly initialize the array

/ / the first method, vararr [3] int= [3] int {1, arr:=, 2, int, 3} / / the second method, arr:= [3], 2, 3}.

The above 3 represents the number of elements in the array. In case you want to add elements to the array one day, you have to modify the number accordingly. In order to avoid this hard coding, you can write this and use. Let the Go language allocate space according to the actual situation.

Arr:= [...] int {1,2,3}

[3] int and [4] int are both arrays, but they are different types. You can find them using% T of fmt. If you compare them with = =, the answer will be false.

Import ("fmt") funcmain () {arr01:= [...] int {1J 2 arr02,arr02 3} arr02:= [...] int {1J 2J 3J 4} fmt.Printf ("% d type is:% T\ n", arr01,arr01) fmt.Printf ("% d type is:% T", arr02,arr02)}

The output is as follows

The type of [1 23] is: [3] int [1 2 3 4] is: [4] int

If you find it a bit troublesome to write [3] int every time, you can define a type literal for [3] int, that is, alias type.

Use the type keyword to define a type literal, and then you can use this alias type as long as you want to define an array with container size 3 and element type int.

Import ("fmt") funcmain () {typearr3 [3] int myarr:=arr3 {1J 2je 3} fmt.Printf ("type of% d is:% T", myarr,myarr)}

The output is as follows

Type of [123] is main.arr3

two。 Slice

A Slice, like an array, is a container that can hold several elements of the same type. Unlike an array, the length of its value cannot be determined by the slice type. Each slice value has an array as its underlying data structure. We also call such an array the underlying array of slices.

A slice is a reference to a contiguous segment of an array, so a slice is a reference type, which can be either the entire array or a subset of items identified by the starting and ending indexes, and it should be noted that the items identified by the terminating index are not included in the slice (meaning that this is a left-closed and right-open interval)

Import ("fmt") funcmain () {myarr:= [...] int {1J 2je 3} fmt.Printf ("type of% d is:% T", myarr [0:2], myarr [0:2])}

The output is as follows

The type of [12] is: [] int

There are three ways to make a slice.

Perform fragment truncation of the array (as shown in the above example: myarr [0:2], 0 is the start value caused by the index, 2 is the end value of the index, and the interval is left-half-right open)

Declare assignments from scratch (examples are as follows)

/ / declare string slice varstrList [] string / / declare integer slice varnumList [] int / / declare an empty slice varnumListEmpty= [] int {}

Constructed using the make function, the format of the make function: make ([] Type, size, cap)

This function just points out that a slice has three properties: type (Type), length (size), and capacity (cap).

Import ("fmt") funcmain () {a:=make ([] int,2) b:=make ([] int,2,10) fmt.Println (a) fmt.Println (len (a), len (b)) fmt.Println (cap (a), cap (b))}

The output is as follows

[00] [00] 22 210

The concepts of len and cap may be difficult to understand. Here's an example:

The name of the company is the variable name. All the stations in the company are equivalent to the allocated memory space of the employees in the company, the equivalent of elements. Cap represents the maximum number of employees your company can accommodate. Len represents how many employees your company currently has, so cap > = len

Because a slice is a reference type, if you don't assign it, its zero value (default) is nil

Varmyarr [] int fmt.Println (myarr==nil) / / true

Arrays have something in common with slices. They are containers that can hold several elements of the same type.

There is also a difference: the container size of the array is fixed, while the slice itself is a reference type, which is more like list in Python, and we can add elements to it append.

Import ("fmt") funcmain () {myarr:= [] int {1} / / append an element myarr=append (myarr,2) / / append multiple elements myarr=append (myarr,3,4) / / append a slice, For unpacking, you cannot omit myarr=append (myarr, [] int {7pr 8}...) / / insert the element myarr=append ([] int {0}, myarr...) / / insert a slice (two elements) myarr=append (myarr [: 5], append ([] int {5J 6}, myarr [5:]...)) fmt.Println (myarr)} in the first position.

The output is as follows

[012345678]

Finally, I'll leave you a question to think about, as follows:

Import ("fmt") funcmain () {varnumbers4= [...] int {1, cap 2, 4, 5, 6, 8, 9, 10} myslice:= numbers 4 [4: 6:8] fmt.Printf ("myslice is% d, its length is:% d\ n", myslice,len (myslice)) myslice=myslice [: cap (myslice)] fmt.Printf ("the fourth element of myslice is:% d", myslice [3])}

Why does myslice have a length of 2 but can access the fourth element?

Myslice is [56], its length is: 2 myslice the fourth element is: 8

Thank you for your reading, the above is the content of "introduction to arrays and slices in Go language". After the study of this article, I believe you have a deeper understanding of the problem of array and slice introduction in Go language, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report