go语言(golang) 数据类型大小以及unsigned和signed类型(go 源码builtin.go)
数据类型定义在源码: C:\Go\src\builtin\builtin.go ,这些数据类型不需要import导入,可直接使用
一、下面是数据类型全部
是文件内部的一些定义
// bool is the set of boolean values, true and false. type bool bool //定义的布尔类型 // true and false are the two untyped boolean values. const ( true = 0 == 0 // Untyped bool. false = 0 != 0 // Untyped bool. ) // uint8 is the set of all unsigned 8-bit integers. // Range: 0 through 255. type uint8 uint8 // uint16 is the set of all unsigned 16-bit integers. // Range: 0 through 65535. type uint16 uint16 // uint32 is the set of all unsigned 32-bit integers. // Range: 0 through 4294967295. type uint32 uint32 // uint64 is the set of all unsigned 64-bit integers. // Range: 0 through 18446744073709551615. type uint64 uint64 // int8 is the set of all signed 8-bit integers. // Range: -128 through 127. type int8 int8 // int16 is the set of all signed 16-bit integers. // Range: -32768 through 32767. type int16 int16 // int32 is the set of all signed 32-bit integers. // Range: -2147483648 through 2147483647. type int32 int32 // int64 is the set of all signed 64-bit integers. // Range: -9223372036854775808 through 9223372036854775807. type int64 int64 // float32 is the set of all IEEE-754 32-bit floating-point numbers. type float32 float32 // float64 is the set of all IEEE-754 64-bit floating-point numbers. type float64 float64 // complex64 is the set of all complex numbers with float32 real and // imaginary parts. type complex64 complex64 // complex128 is the set of all complex numbers with float64 real and // imaginary parts. type complex128 complex128 // string is the set of all strings of 8-bit bytes, conventionally but not // necessarily representing UTF-8-encoded text. A string may be empty, but // not nil. Values of string type are immutable.
type string string //字符串可能是空的,空的话不是nil,nil实际代表的是针对的指针为空来说
// int is a signed integer type that is at least 32 bits in size. It is a // distinct type, however, and not an alias for, say, int32. type int int // uint is an unsigned integer type that is at least 32 bits in size. It is a // distinct type, however, and not an alias for, say, uint32. type uint uint // uintptr is an integer type that is large enough to hold the bit pattern of // any pointer. type uintptr uintptr // byte is an alias for uint8 and is equivalent to uint8 in all ways. It is // used, by convention, to distinguish byte values from 8-bit unsigned // integer values. type byte = uint8 // rune is an alias for int32 and is equivalent to int32 in all ways. It is // used, by convention, to distinguish character values from integer values. type rune = int32 // iota is a predeclared identifier representing the untyped integer ordinal // number of the current const specification in a (usually parenthesized) // const declaration. It is zero-indexed. const iota = 0 // Untyped int. // nil is a predeclared identifier representing the zero value for a // pointer, channel, func, interface, map, or slice type.
//类型必须是一个指针,代表指针为空即0 var nil Type // Type must be a pointer, channel, func, interface, map, or slice type // Type is here for the purposes of documentation only. It is a stand-in // for any Go type, but represents the same type for any given function // invocation. type Type int // Type1 is here for the purposes of documentation only. It is a stand-in // for any Go type, but represents the same type for any given function // invocation. type Type1 int // IntegerType is here for the purposes of documentation only. It is a stand-in // for any integer type: int, uint, int8 etc. type IntegerType int // FloatType is here for the purposes of documentation only. It is a stand-in // for either float type: float32 or float64. type FloatType float32 // ComplexType is here for the purposes of documentation only. It is a // stand-in for either complex type: complex64 or complex128. type ComplexType complex64 // The append built-in function appends elements to the end of a slice. If // it has sufficient capacity, the destination is resliced to accommodate the // new elements. If it does not, a new underlying array will be allocated. // Append returns the updated slice. It is therefore necessary to store the // result of append, often in the variable holding the slice itself: // slice = append(slice, elem1, elem2) // slice = append(slice, anotherSlice...) // As a special case, it is legal to append a string to a byte slice, like this: // slice = append([]byte("hello "), "world"...)
二、特殊类型
广泛的用在各个地方
type Type int //代表任何数据类型 type Type1 int var nil Type //空指针类型,对于指针代表0值(还包括channel, func, interface, map, or slice等都用nil来判断是否为空,空代表着0) const iota = 0 // Untyped int. 只针对const , 代表索引为0 ,他不是int类型 type rune = int32 //rune是int32的别名,用法很多 type byte = uint8 代表字节 FloatType 代表浮点类型包括 float32 或 float64 IntegerType 代表任何整型的数据类型包括 int, uint, int8等 uintptr 代表指针类型,已足够大 ComplexType 代表 complex64 或complex128
三、类型大小按字节
package main
import (
"fmt"
"unsafe"
)
//go支持中文变量、函数以及文件名
func 各个数据类型大小(){
//数据类型和大小定义在 C:\Go\src\builtin\builtin.go
//一、字节维度
//1字节
fmt.Println(unsafe.Sizeof(byte(0))) // 占用1字节
fmt.Println(unsafe.Sizeof(uint8(0))) // 1个字节
//2字节
fmt.Println(unsafe.Sizeof(int16(0))) //占用2字节 最大值: 1111111111111111(65535)
//4字节
fmt.Println(unsafe.Sizeof(int32(0))) // 占用4字节
fmt.Println(unsafe.Sizeof(int8(0))) // 占用4字节
//8字节
fmt.Println(unsafe.Sizeof(float64(0))) //占用8字节
fmt.Println(unsafe.Sizeof(int(0))) //占用8字节
fmt.Println(unsafe.Sizeof(uint(0))) //8个字节,但是源码写的是至少32-bit,但不意味着是32-bit
//16字节
fmt.Println(unsafe.Sizeof(string(0))) //占用16字节(为什么?) 因为底层定义
//二、用前缀
//1.uint类型
fmt.Println(unsafe.Sizeof(uint8(0))) // 1个字节
fmt.Println(unsafe.Sizeof(uint32(0))) //4字节
fmt.Println(unsafe.Sizeof(uint64(0))) //8字节
fmt.Println(unsafe.Sizeof(uint(0))) //8个字节
//2.int类型
fmt.Println(unsafe.Sizeof(int(0))) //占用8字节
fmt.Println(unsafe.Sizeof(int8(0))) //占用4字节
fmt.Println(unsafe.Sizeof(int16(0))) //占用2字节 最大值: 1111111111111111(65535)
fmt.Println(unsafe.Sizeof(int32(0))) // 占用4字节
//3.float
fmt.Println(unsafe.Sizeof(float32(0)))
fmt.Println(unsafe.Sizeof(float64(0)))
//4.byte(比较特殊1个字节-8-Bit)
fmt.Println(unsafe.Sizeof(byte(0))) // 占用1字节(8-bit,1个字节等于8-bit)
//5.代表指针,已足够大
fmt.Println(unsafe.Sizeof(uintptr(0))) //占用8个字节
fmt.Println(unsafe.Sizeof(uint(0))) // 没有打印出来,但是至少是32-bit
}
func main(){
各个数据类型大小()
}
四、unsigned类型一共6个
从范围也可以看出是正整数和0(负整数除外)
// uint8 is the set of all unsigned 8-bit integers. 范围: 0 到 255. type uint8 uint8 // uint16 is the set of all unsigned 16-bit integers. 范围: 0 到 65535. type uint16 uint16 // uint32 is the set of all unsigned 32-bit integers. 范围: 0 到 4294967295. type uint32 uint32 // uint64 is the set of all unsigned 64-bit integers. 范围: 0 到 18446744073709551615. type uint64 uint64
// uint is an unsigned integer type that is at least 32 bits in size. It is a // distinct type, however, and not an alias for, say, uint32.() type uint uint // byte是uint8的别名 and is equivalent to uint8 in all ways. It is // used, by convention, to distinguish byte values from 8-bit unsigned 整型数值. type byte = uint8
五、 signed 类型一共5个
我们初中的时候就学过,实际就是整数,整数包括正整数、0和负整数
// int8 is the set of all signed 8-bit integers. 范围: -128 到127. type int8 int8 // int16 is the set of all signed 16-bit integers.范围: -32768 到 32767. type int16 int16 // int32 is the set of all signed 32-bit integers. 范围: -2147483648 到 2147483647. type int32 int32 // int64 is the set of all signed 64-bit integers. 范围: -9223372036854775808 到9223372036854775807. type int64 int64 // int is a signed integer type that is at least 32 bits in size. It is a // distinct type, however, and not an alias for, say, int32. type int int

浙公网安备 33010602011771号