[Golang]也許有你不知道的,Array和Slice(1)

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

Golang中的array

    在golang中,array是同一類型的元素的有序排列,長度不能更改,佔用記憶體上的一段連續空間。

    1)基礎

    首先看看array的聲明: [plain] view plaincopyprint?
  1. var justiceArray [3]string  
var justiceArray [3]string
 以上聲明了justiceArray是為有3個元素的string數組,括弧裡面的數字是必須的,不能省略。
    另外說明一下,[3]string與[2]string是兩種不同類型的array。
    現在對其賦值:
[plain] view plaincopyprint?
  1. justiceArray = [3]string{"Superman", "Batman", "Wonder Woman"}  
  2. fmt.Printf("The Justice League are: %v\n", justiceArray)  
  3. 輸出:  
  4. The Justice League are: [Superman Batman Wonder Woman]  
justiceArray = [3]string{"Superman", "Batman", "Wonder Woman"}fmt.Printf("The Justice League are: %v\n", justiceArray)輸出:The Justice League are: [Superman Batman Wonder Woman]
    如果你只想建立一個填充預設值的數組,可以這樣:
[plain] view plaincopyprint?
  1. justiceArray = [3]string{}  
  2. fmt.Printf("The Justice League are: %v\n", justiceArray)  
  3. 輸出:  
  4. The Justice League are: [  ]  
justiceArray = [3]string{}fmt.Printf("The Justice League are: %v\n", justiceArray)輸出:The Justice League are: [  ]
    當前數組擁有3個空的字串。
    另外你可以用一種省略形式:
[plain] view plaincopyprint?
  1. justiceArray = [...]string{"Superman", "Batman", "Wonder Woman"}  
  2. fmt.Printf("The Justice League are: %v\n", justiceArray)  
  3. 輸出:  
  4. The Justice League are: [Superman Batman Wonder Woman]  
justiceArray = [...]string{"Superman", "Batman", "Wonder Woman"}fmt.Printf("The Justice League are: %v\n", justiceArray)輸出:The Justice League are: [Superman Batman Wonder Woman]
    用...代替數字,當然大括弧裡的元素需要跟你聲明的數組長度一致。
    目的相同,下面這種聲明賦值就更簡潔了:

[plain] view plaincopyprint?
  1. avengersArray := [...]string{"Captain America", "Hulk"}  
  2. fmt.Printf("The Avengers are: %v\n", avengersArray)  
  3. 輸出:  
  4. The Avengers are: [Captain America Hulk]  
avengersArray := [...]string{"Captain America", "Hulk"}fmt.Printf("The Avengers are: %v\n", avengersArray)輸出:The Avengers are: [Captain America Hulk]
  等號右邊的傳回型別是[2]string。

    2)數組的複製

    需要強調一點的是,array類型的變數指代整個陣列變數(不同於c中的array,後者是一個指標,指向數組的第一元素);
  類似與int這些基本類型,當將array類型的變數賦值時,是複製整個數組,參照下面這個例子:

[plain] view plaincopyprint?
  1. newAvengers := avengersArray  
  2. newAvengers[0] = "Spider-Man"  
  3. fmt.Printf("The old avengers: %v\n", avengersArray)  
  4. fmt.Printf("The new avengers: %v\n", newAvengers)  
  5. 輸出:  
  6. The old avengers: [Captain America Hulk]  
  7. The new avengers: [Spider-Man Hulk]  
newAvengers := avengersArraynewAvengers[0] = "Spider-Man"fmt.Printf("The old avengers: %v\n", avengersArray)fmt.Printf("The new avengers: %v\n", newAvengers)輸出:The old avengers: [Captain America Hulk]The new avengers: [Spider-Man Hulk]
    上面將avengersArray賦值給newAvengers時,是複製了整個數組,而不是單純的指向。

聯繫我們

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