The Go Language array

Source: Internet
Author: User

An array is a data type built into the go language, and for that type, it will be expanded by a few points

Array declaration and initialization

Arrays have the following kinds of ways of declaring initialization

// 方法1var arr [5]int// 方法2var arr = [5]int{1,2,3,4,5}// 方法3arr := [5]int{1,2,3,4,5}// 方法4arr := [...]int{1,2,3,4,5}// 特定赋值arr := [5]int{1:2,4:3}

Array using

    • Array Assignment
var arr = [5]int{}arr[1]=3
    • Array values
var arr = [5]int{5,4,3,2,1}num := arr[1]
    • Array traversal
// for 遍历var arr = [5]int{1,2,3,4,5}for i :=0; i < len(arr); i++ {  fmt.Println(arr[i])} // for range 遍历for _, item := range arr {  fmt.Println(item)}
    • Array type

Arrays of the same length and type are of the same type, and they can be assigned directly between them, assigning values between variable lengths of array variables and requiring a single value

Array pointers

    • Array pointers

An array pointer is a collection of stored pointers

// 声明一个空指针的空数组var pr = [5]*int

So that each array element is a null pointer to a NULL pointer, you cannot give the value direct to a null pointer

Array pass-through parameter

In a previous article, the go language of the transfer of a topic, carried out, do not do a detailed expansion in this
When an array is passed a copy of the array, the modification of the arrays in the function does not result in the change of the array value in the final argument, and if the length of the array is particularly large, the value passed is very memory intensive, so the pointer to the array is passed, it is just a copy of the array pointer, Tangent points to the same array address

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.