Golang Arrays and slices

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1. Arrays

An array in Golang is a data type that consists of fixed-length and fixed-object types. For example, the following:

var a [4]int

A is an array of 4 elements of type int. Once a is declared, the number of elements is fixed and the number of elements does not change within the lifetime of a variable. At this point the type of a is [4]int, if there is also a B variable, for [5]int. Even if the two variables differ only by one element, the memory also occupies a completely different address allocation unit, and A and B are two completely different data types. In Golang, once an array is defined, its internal elements are initialized. That's when a[0] equals 0.
In Golang, an array is a data entity object. In Golang when you use a, you represent the array of a again. In C, when a is used, it represents a pointer to the first element of the array.

2. Slicing

letters := []string{"a", "b", "c", "d"}

An array declaration is required to specify the length of the array or use (...) in square brackets. The symbol automatically calculates the length, and the slice does not need to specify the length of the array. The comparison specification is declared in a way that uses make, roughly in two ways

1, only specify the length, this time the slice length and the same capacity;

2. Specify both the length and the capacity of the slice.

var s1 = make([]byte, 5)var s2 = make([]byte, 5, 10)

Since a slice is a reference type, all other references change that value when the reference changes the value of the element. For example

var a = []int{1, 2, 3, 4, 5, 6, 7, 8, 9}s1 := a[:4]s2 := a[3:7]fmt.Println(s1)fmt.Println(s2)s1[3] = 100fmt.Println(s1)fmt.Println(s2)

The result is:

[1 2 3 4]

[4 5 6 7]

[1 2 3 100]

[100 5 6 7]

Conceptually, a slice is like a struct, containing three elements:

1, a pointer to the array to specify the starting position of the slice;

2, length, that is, the length of the slice, through the built-in function Len obtained;

3, maximum length, that is, the maximum size of the slice, obtained through the built-in function cap.

If Len is larger than the cap, then a run-time exception is triggered.

Golang provides the Append function to add elements, and when the Append function is used, the APPEND function determines whether the destination slice has the remaining space, and if there is no space left, it automatically expands twice times the space.

Golang provides copy for copying content from one array slice to another array slice. If you add two array slices that are not the same size, they will be copied by the number of elements in the smaller array.

slice1 := []int{1, 2, 3, 4, 5} slice2 := []int{5, 4, 3} copy(slice2, slice1) // 只会复制slice1的前3个元素到slice2中 copy(slice1, slice2) // 只会复制slice2的3个元素到slice1的前3个位置

Detailed sections

http://www.jianshu.com/p/030a ...

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.