Golang系列之布爾類型基本介紹
1、基本介紹
go語言中的布爾類型,關鍵字為bool,布爾類型隻有true或者false兩種類型,占用1個位元組,布爾類型适用于邏輯運算,一般在流程控制語句中,比如if條件、for循環等等
var bol bool = true
fmt.Println("bol=" , bol)
2、布爾類型位元組
布爾類型占用記憶體空間大小是1個位元組,可以通過
unsafe.Sizeof
列印
var bol bool = true
// 列印布爾類型占用位元組
fmt.Println("size of bool=" , unsafe.Sizeof(bol))
3、不支援類型強轉
var b bool
// 編譯會報錯,cannot use 1 (type untyped int) as type bool in assignment
b = 1
// 類型強轉也會報錯,cannot convert 1 (type untyped int) to type bool
b = bool(1)
var bolv bool
bolv := (1!=2)
fmt.Println("b11=" , bolv)