funcmain(){var a bool=truevar b bool=falsevar c =true
d :=false
fmt.Println(a, b, c, d)}
使用unsafe包下的Sizeof()可以查看类型占用字节
funcmain(){
a :=false
fmt.Println(unsafe.Sizeof(a))}
虽然bool类型占用一个byte,但是bool不能和byte或int8相互转换
funcmain(){var a int8=1var b byte=0var c bool=false
fmt.Println(a, b, c)
a =int8(c)//cannot convert c (type bool) to type int8
b =byte(c)//cannot convert c (type bool) to type byte
c =bool(a)//cannot convert a (type int8) to type bool
c =bool(b)//cannot convert b (type byte) to type bool
b =byte(a)//可以}
布尔类型除了直接赋值true或false以外,还是可以表达式赋值,借助比较运算符、逻辑运算符等
funcmain(){
a :=5>3
fmt.Println(a)//输出:true
fmt.Printf("%T", a)//输出:bool}
一.布尔类型介绍布尔类型关键字bool布尔类型可取值只有两个true :代表真,表示成立,二进制表示时1表示真false:代表假,表示不成立,二进制表示时0表示假布尔类型不能与其他类型相互转换布尔类型占用1个byte布尔类型单独使用较少,多用在判断中二. 布尔类型代码示例创建bool类型变量func main() { var a bool = true var b bool = false var c = true d := false fmt.Println(a