Go 类型定义

Go 类型定义,第1张

类型

type IZ int

var a IZ = 5

type (  IZ int  FZ float64  STR string)

valueOfTypeB = typeB(valueOfTypeA)

var a IZ =5c :=int(a)d := IZ(c)

显式类型定义: const b string = "abc"隐式类型定义: const b = "abc"

var n intf(n +5)// 无类型的数字型常量 “5” 它的类型在这里变成了 int

正确的做法:const c1 = 2/3错误的做法:const c2 = getNumber() // 引发构建错误: getNumber() used as value

constLn2=0.693147180559945309417232121458\176568075500134360255254120680009constLog2E=1/Ln2// this is a precise reciprocalconstBillion=1e9// float constantconst hardEight =(1<<100)>>97

const beef, two, c ="eat",2,"veg"constMonday,Tuesday,Wednesday,Thursday,Friday,Saturday=1,2,3,4,5,6const(Monday,Tuesday,Wednesday=1,2,3Thursday,Friday,Saturday=4,5,6)

const(Unknown=0Female=1Male=2)

// 赋值一个常量时,之后没赋值的常量都会应用上一行的赋值表达式const(    a = iota  // a = 0    b         // b = 1    c         // c = 2    d =5// d = 5       e         // e = 5)

// 赋值两个常量,iota 只会增长一次,而不会因为使用了两次就增长两次const(Apple,Banana= iota +1, iota +2// Apple=1 Banana=2Cherimoya,Durian// Cherimoya=2 Durian=3Elderberry,Fig// Elderberry=3, Fig=4)

// 这里的两种表达方式比较特殊

// 使用 iota 结合 位运算 表示资源状态的使用案例const(Open=1<< iota  // 0001Close// 0010Pending// 0100)const(    _           = iota             // 使用 _ 忽略不需要的 iota    KB =1<<(10* iota)// 1 << (10*1)    MB                             // 1 << (10*2)    GB                             // 1 << (10*3)    TB                             // 1 << (10*4)    PB                             // 1 << (10*5)    EB                             // 1 << (10*6)    ZB                             // 1 << (10*7)    YB                             // 1 << (10*8))

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/994968.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存