重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
這篇文章主要介紹“Go Type的使用場景是什么”,在日常操作中,相信很多人在Go Type的使用場景是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Go Type的使用場景是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
目前成都創新互聯已為上1000+的企業提供了網站建設、域名、網絡空間、網站運營、企業網站設計、吐魯番網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發展。
Go Type 使用場景
type 使用場景
1. 定義結構體
// 定義商標結構 //將Brand定義為如下的結構體類型 type Brand struct { } // 為商標結構添加Show()方法 func (t Brand) Show() { }
2. 作別名
在 Go 1.9 版本之前定義內建類型的代碼是這樣寫的:
type byte uint8 type rune int32
而在 Go 1.9 版本之后變為:
type byte = uint8 type rune = int32
區分類型別名與類型定義
// 將NewInt定義為int類型 type NewInt int // 將int取一個別名叫IntAlias type IntAlias = int func main() { // 將a聲明為NewInt類型 var a NewInt // 查看a的類型名 fmt.Printf("a type: %T\n", a) // 將a2聲明為IntAlias類型 var a2 IntAlias // 查看a2的類型名 fmt.Printf("a2 type: %T\n", a2) } a type: main.NewInt a2 type: int
批量定義結構體
type ( // A PrivateKeyConf is a private key config. PrivateKeyConf struct { Fingerprint string KeyFile string } // A SignatureConf is a signature config. SignatureConf struct { Strict bool `json:",default=false"` Expiry time.Duration `json:",default=1h"` PrivateKeys []PrivateKeyConf } )
單個定義結構體
type PrivateKeyConf struct { Fingerprint string KeyFile string } type SignatureConf struct { Strict bool `json:",default=false"` Expiry time.Duration `json:",default=1h"` PrivateKeys []PrivateKeyConf }
到此,關于“Go Type的使用場景是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注創新互聯網站,小編會繼續努力為大家帶來更多實用的文章!