【go语言 基础系列】基础数据类型及定义

【基本分类】

go语言的数据类型分四大类

    基础类型    数字 number 字符串string 布尔boolean
    聚合类型    数组 array   结构体struct
    引用类型    指针pointer  slice  map function channel
    接口类型    interface

【基础类型】

  基础类型大致可分3类

  数值类:整型(有无符号)、浮点型、复数

  布尔型: true  false

  字符串

【类型定义】

 各个类型的定义、取值范围以源码 builtin.go为准

无符号整型主要有:uint8   uint16    uint32   uint64

 22 // uint8 is the set of all unsigned 8-bit integers.
 23 // Range: 0 through 255.
 24 type uint8 uint8
 25 
 26 // uint16 is the set of all unsigned 16-bit integers.
 27 // Range: 0 through 65535.
 28 type uint16 uint16
 29 
 30 // uint32 is the set of all unsigned 32-bit integers.
 31 // Range: 0 through 4294967295.
 32 type uint32 uint32
 33 
 34 // uint64 is the set of all unsigned 64-bit integers.
 35 // Range: 0 through 18446744073709551615.
 36 type uint64 uint64

有符号整型主要有:int8   int16     int32    int64

 38 // int8 is the set of all signed 8-bit integers.
 39 // Range: -128 through 127.
 40 type int8 int8
 41 
 42 // int16 is the set of all signed 16-bit integers.
 43 // Range: -32768 through 32767.
 44 type int16 int16
 45 
 46 // int32 is the set of all signed 32-bit integers.
 47 // Range: -2147483648 through 2147483647.
 48 type int32 int32
 49 
 50 // int64 is the set of all signed 64-bit integers.
 51 // Range: -9223372036854775808 through 9223372036854775807.
 52 type int64 int64

浮点型有两类,float32  float64

54 // float32 is the set of all IEEE-754 32-bit floating-point numbers.
 55 type float32 float32
 56 
 57 // float64 is the set of all IEEE-754 64-bit floating-point numbers.
 58 type float64 float64

复数

 60 // complex64 is the set of all complex numbers with float32 real and
 61 // imaginary parts.
 62 type complex64 complex64
 63 
 64 // complex128 is the set of all complex numbers with float64 real and
 65 // imaginary parts.
 66 type complex128 complex128

布尔类型

 13 // bool is the set of boolean values, true and false.
 14 type bool bool
 15 
 16 // true and false are the two untyped boolean values.
 17 const (
 18         true  = 0 == 0 // Untyped bool.
 19         false = 0 != 0 // Untyped bool.
 20 )

字符串

字符串值无法改变:字符串值 本身所包含的字节序列 永不可变。即 字符串内部的数据不允许修改

不可变 意味着 两个字符串能安全的共用 同一段底层内存,使得 复制任何长度 字符串的开销都很低廉

 68 // string is the set of all strings of 8-bit bytes, conventionally but not
 69 // necessarily representing UTF-8-encoded text. A string may be empty, but
 70 // not nil. Values of string type are immutable.
 71 type string string

【类型转换】

    对于每种类型T ,如果可以转换, 操作T(x) 会把 x的值 转换成类型T
    整型与浮点型的相互转换  可能会改变值 或者损失精度

猜你喜欢

转载自blog.csdn.net/natpan/article/details/83541158