Golang中的布尔(bool)类型
go语言中的布尔类型不能接受其他类型的赋值,不支持自动或强制的类型转换。
var a bool
a = true
b := true
c := (1==1)
上面这几种方法都正确。
var a bool
a = 1
a = bool(1)
这种方法将引发编译错误: cannot use 1 (type int) as type bool in assignment 。
正确的方法应该为:
var a bool
a = (1!=0)
如需转载,请注明出处;本文地址:https://www.perfcode.com/p/1471.html