GoType的使用场景是什么
这篇文章主要介绍“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的使用场景是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
分享文章:GoType的使用场景是什么
文章链接:http://www.jxjierui.cn/article/ihsccj.html


咨询
建站咨询
