Go Language Learning Notes (1)-arrays and slices

Source: Internet
Author: User

Data (array) and slices (slice)

Array declaration:

ArrayType??? = "[" Arraylength "]" ElementType.

For example:

var a [+] int

var b [3][5] int

?

In Go and C, there are several important differences in how arrays work. In Go,

(1) An array is a value type. Assigning an array to another will copy all the elements.

(2)? If you pass an array to the function, it will receive a copy of the array instead of its pointer.

(3) The size of the array is part of its type, and the type [10]int and [20]int are different. After the array length is declared, it cannot be changed.

?

Slice declaration:

Slicetype = "["] "ElementType".

For example:

var a []int

The slice without initialization is nil.

?

Slices (slice) are encapsulated in arrays, in effect, a slice can be seen as an array of dynamically changing sizes, similar to std::vector in C + +. Just as std::vector is used extensively in actual C + + programming, most of the array programming in GO is done through slices, not simple arrays.

?

In general, there are two ways to initialize a slice:

(1) by array

var MyArray [10]int = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

var myslice []int = Myarray[:5]

?

(2) by Make

Grammar:

Make ([]t, length, capacity)

?

Create an initial length of 5 with a capacity of 10 as a slice, and each element of the slice is 0:

Slice1: = Make ([]int, 5, 10)

?

Create a slice of length 5 and initialize each element of the slice:

Slice2: = []int{1, 2, 3, 4, 5}

?

For slices, the most important feature is that the length is variable:

Slice2: = []int{1, 2, 3, 4, 5}

Fmt. Println ("Slice:", slice2)

?

Slice2 = Append (Slice2, 6)

Fmt. Println ("Slice:", slice2)

?

Output:

Slice: [1 2 3 4 5]

Slice: [1 2 3 4 5 6]

?

Function append is a built-in function that go provides for adding elements to a slice.

?

The slice holds a reference to the underlying array, and if you assign one slice to another, both will reference the same array. If a function takes a slice as an argument, the changes it makes to the elements of the slice are visible to the caller, like a pointer to the underlying array.

?

Func (f *file) Read (b []byte) (n int, err error)

This OS. File's Read method, which takes a slice parameter, and the length of the slice already sets the upper limit of the data to read. A pointer to a buffer, and the size of the buffer, are required for C/C + +:

int read (file* F, char* buf, int len)

As you can see from here, go is easier to read.


YY brother?
Source: http://www.cnblogs.com/hustcat/?
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Go Language learning Note (1)-arrays and slices

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.