go 的基本数据类型

水滴石川1 · · 433 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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 

有疑问加站长微信联系(非本文作者)

本文来自:51CTO博客

感谢作者:水滴石川1

查看原文:go 的基本数据类型

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

433 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传