kubernetes學習筆記----- Go語言中structTag

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

在讀kubernetes源碼的時候,讀到https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/extensions/types.go   k8s的type的時候,遇到這樣一段代碼,

// represents a scaling request for a resource.type Scale struct {unversioned.TypeMeta `json:",inline"`// Standard object metadata; More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#metadata.api.ObjectMeta `json:"metadata,omitempty"`// defines the behavior of the scale. More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#spec-and-status.Spec ScaleSpec `json:"spec,omitempty"`// current status of the scale. More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#spec-and-status. Read-only.Status ScaleStatus `json:"status,omitempty"`}

然後後面的 `` 裡面的 json字串,我就不明白了


這是Go語言的structTag 


1 幹什麼用的? 

如果希望手動設定結構體的成員和JSON欄位的對應關係,可以在定義結構體的時候給成員打標籤:

使用omitempty熟悉,如果該欄位為nil或0值(數字0,字串"",空數組[]等),則打包的JSON結果不會有這個欄位。


2 怎麼用啊 ?   http://blog.csdn.net/tiaotiaoyly/article/details/38942311點擊開啟連結

 type Message struct {Name string `json:"msg_name"`       // 對應JSON的msg_nameBody string `json:"body,omitempty"` // 如果為空白置則忽略欄位Time int64  `json:"-"`              // 直接忽略欄位}var m = Message{Name: "Alice",Body: "",Time: 1294706395881547000,}data, err := json.Marshal(m)if err != nil {fmt.Printf(err.Error())return}fmt.Println(string(data))Output:{"msg_name":"Alice"}


相關文章

聯繫我們

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