golang:聊聊关于Go Type的使用场景

 所属分类:php教程

 浏览:107次-  评论: 0次-  更新时间:2022-09-30
描述:更多教程资料进入php教程获得。 本文由go语言教程栏目给大家介绍关于Go Type的使用场景 ,希望对需要的朋友有所帮助!Go Type 使用场景...
更多教程资料进入php教程获得。 本文由go语言教程栏目给大家介绍关于Go Type的使用场景 ,希望对需要的朋友有所帮助!

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
}

php入门到就业线上直播课:立即学习
全程直播 + 实战授课 + 边学 + 边练 + 边辅导

以上就是聊聊关于Go Type的使用场景的详细内容,更多请关注zzsucai.com其它相关文章!

 标签: go,golang,go教程,
积分说明:注册即送10金币,每日签到可获得更多金币,成为VIP会员可免金币下载! 充值积分充值会员更多说明»

讨论这个素材(0)回答他人问题或分享使用心得奖励金币

〒_〒 居然一个评论都没有……

表情  文明上网,理性发言!