Go语言Type的使用场景的什么,用法是怎样
导读:相信很多人对“Go语言Type的使用场景的什么,用法是怎样”都不太了解,下面小编为你详细解释一下这个问题,希望对你有一定的帮助 Go Type 使用场景type 使用场景1. 定义结构体// 定义商标结构 //将Brand定义为如下的结...
相信很多人对“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 }
到此这篇关于“Go语言Type的使用场景的什么,用法是怎样”的文章就介绍到这了,感谢各位的阅读,更多相关Go语言Type的使用场景的什么,用法是怎样内容,欢迎关注网络资讯频道,小编将为大家输出更多高质量的实用文章!
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Go语言Type的使用场景的什么,用法是怎样
本文地址: https://pptw.com/jishu/654781.html