Go language learning notes 2 -- array and slice

Source: Internet
Author: User
Tags array definition python list

In any language, the array should be a very basic type, and the usage should be high. Go not only provides arrays, but also adds a layer of packaging on the array type. This packaging is also called slice.

The array variable of Go (that is, the array name) is a real array, rather than a pointer to the starting position of the array memory, and cannot be converted to the same type of pointer, this is seriously different from the C language. The C language is mainly because the type system is too weak, so a lot of data is eventually degraded to pointer access.


Go array definition:

var a [10]intvar a = [10]int{0,1,2,3,4,5,6,7,8,9}var a = [...]int{0,1,2,3,4,5,6,7,8,9}var a = [2][2]int{[2]int{1,1}, [2]int{2,2}}var a = [2][2]int{{1,1}, {2,2}}

The preceding array definition shows that the array type of Go consists of two parts: type and length. An array is a continuous memory space that stores elements of the same type. Therefore, the type of an array must be determined by the type of the elements stored and the number of elements stored. This seems to be the same in all languages, and none of them appear.

As mentioned above, slice is encapsulated on top of an array. Therefore, each slice has a hard array; just like a arrogant woman, there is always a good man behind it. I think slice is also arrogant and can always manipulate the array behind it as needed.


Let's take a look at what the slice looks like:

var a = [10]int{0,1,,2,3,4,5,6,7,8,9}var s1 = a[0:5]var s2 = a[0:]var s3 = a[:]

We can see that slice can represent one or more elements in an array as you like. More importantly, slice is not a value type, but a reference type, just like a pointer, write operations on the elements obtained by slice actually affect the underlying array layer. Slice is actually a thin encapsulation of operations. Therefore, slice has an important purpose as a function parameter, which can avoid a memory copy of the entire array by directly passing the array.

In go programming, we advocate using slice to operate arrays, rather than directly operating bare arrays, because slice looks more elegant and features are closer to the pointers in C language.

Slice is not a go patent. There are many languages that support this feature. At least the python List supports such a sharding operation. After careful observation, we can find that these dynamic languages have some excellent features that traditional languages do not have in recent years. Go not only has these features, but also is a compiled static language. This also shows that software productivity is also improving, but a large number of programmers seem to not want to move their own steps, just a taste of the new taste, especially a bunch of C programmers with complex feelings, I am here, too.

Add a code to show the relationship between arrays and slice:

package mainimport "fmt"func main() {    a := []int{0,1,2,3,4,5,6,7,8,9}    s := a[0:5]    Print(a, "a")    Print(s, "s")    s1 := append(s, 0)    fmt.Printf("=================\n")    Print(a, "a")    Print(s, "s")    Print(s1, "s1")}func Print(o []int, name string) {    fmt.Printf("%s: ", name)    for _, v := range o {        fmt.Printf("%d ", v)    }    fmt.Printf("\n")} 

Running result:

A: 0 1 2 3 4 5 6 7 8 9
S: 0 1 2 3 4
========================
A: 0 1 2 3 406 7 8 9
S: 0 1 2 3 4
S1: 0 1 2 3 4 0

This code is an experiment designed to deepen your understanding. I think I can still see some clues.

Note: The go language learning notes series are just a few notes and insights you have taken, not textbooks.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.