go 的基本数据类型
source link: https://studygolang.com/articles/25816
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
go 支持的数据类型
bool 类型
数字类型
有符号整型
无符号整型
浮点型
复数类型
//bool 类型
//bool 表示布尔值,值为true 或者false
func booltest() { x :=true y :=false fmt.Println("x",x,"y",y) } 输出:x true y false
数字类型分为:有符号整型、无符号整型、
有符号整型
int 8 表示8位 有符号整型
范围 -128~127
int 16 表示16位有符号整型
说值范围 -32768~32767
int32 表示32 位有符号整型
范围 -2147483648~2147483647
int64 表示64位有符号整型
-9223372036854775808~9223372036854775807
int 根据不同底层平台,表示32位或者64位整型 ,除非对整型大小有特定对的需求
在32位系统是32位,64 位系统是64位
无符号整型
unit8:
数值范围:0-255
unit16:
数值范围:0-65535
unit32
数值范围:0~4294967295
unit64:
数值范围:0~18446744073709551615
unit:根据不同的底层平台,32 位系统是32位,64 位系统是64位
var x1 int =67 y1 :=88 fmt.Println("value of x1 is ",x1 ,"y1 is ",y1) var x2 int = 110 y2 :=78 fmt.Println("x2 is", x2,"y2 is ",y2) // go 的格式化输出 ,常用的 // %T 输出 Go 语言语法格式的类型和值 // %d 整型以十进制方式显示 // %v 按值的本来值输出 fmt.Printf("type os x2 is %T,size of x2 is %d",x2,unsafe.Sizeof(x2)) fmt.Printf("\ntype os y2 is %T,size of y2 is %d",y2,unsafe.Sizeof(y2)) 输出: value of x1 is 67 y1 is 88 x2 is 110 y2 is 78 type os x2 is int,size of x2 is 8 # 说明平台是64位操作系统 type os y2 is int,size of y2 is 8
// 浮点型
float32: 32 位浮点型
float64: 64位浮点数
x3 ,y3 := 18.44, 9.43 fmt.Printf("type of x3 %T y3 %T\n",x3,y3) sum := x3 + y3 jian := x3 - y3 fmt.Println("sum is ",sum,"xiang jian is ",jian) 输出: type of x3 float64 y3 float64 sum is 27.87 xiang jian is 9.010000000000002
字符串类型
one :="zhangsan" two :="lisi" three :="wangwu" all_name := one + two + three fmt.Println("all nam connect is",all_name) 输出 all nam connect is zhangsanlisiwangwu }
复数类型
complex64:实部和虚部都是 float32 类型的的复数。
complex128:实部和虚部都是 float64 类型的的复数。
类型转换
go 语言没有自动类型提升或者类型转换
a :=32 b :=45.3 sum := a + b fmt.Println(sum) 输出报错: bao cuo src/20190104/类型1.go:128:11: invalid operation: a + b (mismatched types int and float64)
类型转换
a1 :=32 b1 :=45.3 sum := a1 + int(b1) fmt.Println(sum) 输出 77
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK