go 速學 - 06 - 數組

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

目錄

    • 數組
      • 建立數組
      • 數組的比較
      • 數組與指標
        • 指向數組的指標
        • 數組指標
      • New
      • 多維陣列

摘要

建立數組(指定長度,不指定長度,指定索引),數組指標與指向數組的指標,new

數組

建立數組

直接指定長度

var score [3]int    //[0 0 0]

指定長度並初始化

score1 := [2]int{3, 4} //[3 4]

利用索引初始化

score2 := [10]int{3: 4, 9: 1} //[0 0 0 4 0 0 0 0 0 1]

不直接指定長度

score3 := [...]int{1, 2, 3, 4, 5} //[1 2 3 4 5]

數組的比較

Go 中數組為實值型別,可以直接使用 == 比較

數組與指標

指向數組的指標

var pscore *[5]int = &score3 //&[1 2 3 4 5]

數組指標

a := 1b := 2pscore2 := [...]*int{&a, &b} //[0x20818a2c0 0x20818a2c8]

New

可以使用 new 建立數組,此方法返回一個指向數組的指標

p := new([10]int) //&[0 0 0 0 0 0 0 0 0 0]

多維陣列

arr := [2][3]int{{1, 1, 1}, {2, 2, 2}} //[[1 1 1] [2 2 2]]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.