【Go學習筆記】10、結構struct

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

結構struct

  • Go中的struct與c中的struct非常相似,並且Go沒有class
  • 使用typestruct{} 定義結構,名稱遵循可見度規則
  • 支援指向自身的指標類型成員
  • 支援匿名結構,可用作成員或定義成員變數
  • 匿名結構也可以用於map的值
  • 可以使用字面值對結構進行初始化
  • 允許直接通過指標來讀寫結構成員
  • 相同類型的成員可進行直接拷貝賦值
  • 支援 == 與 != 比較子,但不支援 > 或 <
  • 支援匿名欄位,本質上是定義了以某個類型名為名稱的欄位
  • 嵌入結構作為匿名欄位看起來像繼承,但不是繼承
  • 可以使用匿名欄位指標
1234567891011121314151617181920212223242526272829
type human struct{Sex int}//使用組合//類似繼承type teacher struct {humanName stringAge int}type student struct {humanName stringAge int}func main(){a := teacher{Name:"joe",Age:19,human:human{Sex:0}}a := student{Name:"joe",Age:20,human:human{Sex:1}}a.Name = "joe2"a.Age = 13a.Sex = 100}

聯繫我們

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