Golang Learning notes 5--Arrays Array

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

Definition of 1.array

    • To define the format of an array:
var a[4]int  //元素自动初始化为零a := [...]int{19:1}  //编译器按照初始化值数量确定数组长度a := [5]int{1,2}  //未提供初始值的元素自动初始化为零
    • Array lengths are also part of a type, so arrays with different lengths are of different types

    • Array is a value type in Go

2. Array pointers and pointer arrays

//数组指针是指获取数组变量的地址。//此时变量p就是指向数组的指针。特别注意p定义的类型为长度为100的数组的指针。 长度必须相等才能赋值。func main() {    var a =  [...]int{99:1}    var p *[100]int = &a    fmt.Println(p)}  //指针数组是指元素为指针类型的数组func main() {    var x, y = 2, 3    var a  = [...]*int{&x, &y}    fmt.Println(a)}

3. Comparisons between arrays

You can use = = or between arrays! = Compare, but cannot use < or >

//数组类型必须相同才能比较func main() {  a := [2]int{1,2}  b := [2]int{1,3}  fmt.Println(a == b)}

4. Using new to create an array, this method returns an array pointer

func main() {  p := new([10]int)  fmt.Println(p)}

5. Multidimensional arrays

func main() {    a := [2][3]int{        {1,2,3},        {4,5,6}    }    b := [2][3]int{        {1:1},        {2:2}    }    c := [...][3]int{        {1:1},        {2:2}    } }

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.