溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Go Type的使用場景是什么

發(fā)布時(shí)間:2021-10-26 17:33:01 來源:億速云 閱讀:161 作者:iii 欄目:編程語言

這篇文章主要介紹“Go Type的使用場景是什么”,在日常操作中,相信很多人在Go Type的使用場景是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”Go Type的使用場景是什么”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

Go Type 使用場景

type 使用場景

1. 定義結(jié)構(gòu)體

// 定義商標(biāo)結(jié)構(gòu)
//將Brand定義為如下的結(jié)構(gòu)體類型
type Brand struct {
}
// 為商標(biāo)結(jié)構(gòu)添加Show()方法
func (t Brand) Show() {
}

2. 作別名

在 Go 1.9 版本之前定義內(nèi)建類型的代碼是這樣寫的:

type byte uint8
type rune int32

而在 Go 1.9 版本之后變?yōu)椋?/p>

type byte = uint8
type rune = int32

區(qū)分類型別名與類型定義

// 將NewInt定義為int類型
type NewInt int
// 將int取一個(gè)別名叫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

批量定義結(jié)構(gòu)體

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
    }
)

單個(gè)定義結(jié)構(gòu)體

type PrivateKeyConf struct {
    Fingerprint string
    KeyFile     string
}
type SignatureConf struct {
    Strict      bool          `json:",default=false"`
    Expiry      time.Duration `json:",default=1h"`
    PrivateKeys []PrivateKeyConf
}

到此,關(guān)于“Go Type的使用場景是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

go
AI