学习《GO语言学习笔记》的学习笔记之2.6 类型转换 (详解)

版权声明:诸葛老刘所有 https://blog.csdn.net/weixin_39791387/article/details/87291825

本文适合初学者阅读

  • 隐式转换造成的问题远大于它带来的好处.
  • 除常量, 别名类型以及未命名类型外, GO 强制要求使用显式类型转换.
  • GO不支持操作符重载, 所以我们总是能确定语句及表达式的明确含义
a := 10
b := byte(a)
c := a + int(b) // 混合类型表达式必须确保类型一致

同样不能将非bool类型结果当作true/false使用.(python中是可以的)

func main() {
    x := 100
    var b bool = x   // 报错: cannot use x (type int) as type bool in assignment
    if x {   // 报错: non-bool x (type int) used as if condition
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39791387/article/details/87291825