go語言type使用小技巧

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
import "fmt"type Rank int const (        Rank001 Rank = iota        Rank002        Rank003        Rank004)var rewards = [...]string{        "獎勵1","獎勵2","獎勵3","獎勵4",}        func (r Rank) getReward() string {        return rewards[r]}func main() {                fmt.Println(Rank003.getReward())}


通過這樣的方式,靈活的將排名與獎勵對應,並且“物件導向”,將排名,這個int型別宣告為排名,並為其新增成員函數。常規方式在C/C++,Java中,就直接通過2個以維數組驚醒處理,但相對而言,go語言這種方式,真的很贊!




go time/time.go 中:

   108type Weekday int   109   110const (   111Sunday Weekday = iota   112Monday   113Tuesday   114Wednesday   115Thursday   116Friday   117Saturday   118)   119   120var days = [...]string{   121"Sunday",   122"Monday",   123"Tuesday",   124"Wednesday",   125"Thursday",   126"Friday",   127"Saturday",   128}   129   130// String returns the English name of the day ("Sunday", "Monday", ...).   131func (d Weekday) String() string { return days[d] }

相關文章

聯繫我們

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