15

go 的基本数据类型

 5 years ago
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.
neoserver,ios ssh client

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

  • 54

        Java中的数据类型有两种,基本数据类型和引用数据类型,引用数据类型的创建是需要去new一个对象,该对象的内存分配在堆区,同时栈区会保存一个指向该对象的引用,但是对于一些简单数据的创建,用new的方式就不是很有效了...

  • 92
    • www.10tiao.com 6 years ago
    • Cache

    基本数据类型转换

    用字符数据类型变量接收一个整型数据会输出什么?

  • 69
    • studygolang.com 6 years ago
    • Cache

    Go语言2-基本数据类型和操作符

    主要内容: 文件名、关键字、标识符 Go程序的基本结构 常量和变量 数据类型和操作符 字符串类型 文件名、关键字、标识符 所有go源码以.go结尾

  • 37
    • www.tuicool.com 5 years ago
    • Cache

    python中基本的数据类型

    基本数据类型 数据:描述衡量数据的状态 类型:不同的事物需要不同的类型存储 整型  int 定义:年龄,手机号码等是整数的数字 字...

  • 22
    • studygolang.com 5 years ago
    • Cache

    Golang:基本的数据类型

    golang中有丰富的数据类型,除了基本的整型、浮点型、布尔型、字符串外,还有数组、切片、结构体、函数、map、channel等 基本数据类型 整型 整型分为两大类: 按长度:int8、int16、int32、int64...

  • 27
    • studygolang.com 5 years ago
    • Cache

    Go基本数据类型

    Go语言中给我们提供的所有基本数据类型,我们应该理解如何在自己的Go程序中使用这些类型。 数据类型 Go语言将数据类型分为四类:Basic Types(基础类型)、Aggregate Types (复合类型)、Reference Types (引用类型)...

  • 24

    一、基本类型 1、基本类型 不使用New创建,声明一个非引用传递的变量,且变量的值直接置于堆栈中,大小不随运行环境变化,效率更高。使用new创建的引用对象存储在堆中。 2、基本...

  • 34
    • cbaj.gitee.io 4 years ago
    • Cache

    reids基本数据类型

    Redis 有 5 种基础数据结构,分别为:string (字符串)、list (列表)、set (集合)、hash (哈希) 和 zset (有序集合)。 String(字...

  • 10
    • suiyia.github.io 4 years ago
    • Cache

    Java 基本数据类型与包装类

    Java 基本数据类型与包装类 2019-10-13

  • 14

    JS基本数据类型和引用数据类型的区别

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK