栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

聊聊关于Go Type的使用场景

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

聊聊关于Go Type的使用场景

本文由go语言教程栏目给大家介绍关于Go Type的使用场景 ,希望对需要的朋友有所帮助!

Go Type 使用场景

type 使用场景

1. 定义结构体

// 定义商标结构//将Brand定义为如下的结构体类型type Brand struct {}// 为商标结构添加Show()方法func (t Brand) Show() {}

2. 作别名

在 Go 1.9 版本之前定义内建类型的代码是这样写的:

type byte uint8type rune int32

而在 Go 1.9 版本之后变为:

type byte = uint8type rune = int32

区分类型别名与类型定义

// 将NewInt定义为int类型type NewInt int// 将int取一个别名叫IntAliastype IntAlias = intfunc main() {    // 将a声明为NewInt类型    var a NewInt    // 查看a的类型名    fmt.Printf("a type: %Tn", a)    // 将a2声明为IntAlias类型    var a2 IntAlias    // 查看a2的类型名    fmt.Printf("a2 type: %Tn", a2)}a type: main.NewInta2 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的使用场景的详细内容,更多请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/793724.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号