golang array, slice, string筆記

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。本來想寫一篇關於golang io的筆記,但是在學習io之前必須瞭解array, slice, string概念,因此將在下篇寫golang io. array:數組的長度是該數群組類型的一部分,例如var buffer [256]byte 的類型是[256]byte, len(buffer)總是返回256. slice:一個slice描述一個數組中連續的一部分,例如var slice = buffer[100:250]。slice也可以從slice產生,如var slice2 = slice[5:10],其中slice的type是[]byte。slice並不真正儲存資料,它只是包含一個指向資料的pointer,你可以把它當成這樣的一個結構:
type sliceHeader struct {    Length        int
    Capacity      int    ZerothElement *byte}
其中Capacity等於slice所指的數組的大小減去ZeroElement的index。我們還可以用make函數來建立slice,slice := make([]int, 10, 15)其中第二個參數是slice的長度,第三個參數是slice的Capacity。或者用直接量slice := []int{0, 1, 2, 3, 4}。
string:可以把string看作一個唯讀byte slice。例如var sample string = "\xdb\xb2\x3d" 是一個長度為3的byte slice.對於其中非ASCII的unicode資料將被格式化為UTF-8。Code point指一個unicode值,由幾個byte組成。一個character字元可以由幾個Code point,甚至不同的Code point組成。golang使用rune來代表code point,類型為int32,在golang中character都被normalize成一個rune。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.