GO语言自学_007_基本数据类型

一、基本数据类型

1、布尔型
数据类型 大小
bool 1字节
2、数值型
(1) 整型
数据类型 大小 范围
int 【32位系统】4字节【64位系统】8字节 【32位系统】-2,147,483,648~2,147,483,647 【64位系统】-9,223,372,036,854,775,808~9,223,372,036,854,775,807
int8 1字节 -128~127
int16 2字节 -32,768~32,767
int32 4字节 -2,147,483,648~2,147,483,647
in64 8字节 -9,223,372,036,854,775,808~9,223,372,036,854,775,807
uint 【32位系统】4字节【64位系统】8字节 【32位系统】0~4,294,967,295 【64位系统】0~18,446,744,073,709,551,615
uint8 1字节 0~255
uint16 2字节 0~65,535
uint32 4字节 0~4,294,967,295
uint64 8字节 0~18,446,744,073,709,551,615
(2) 浮点型
数据类型 大小 范围
float32 4字节 1.4e-45~3.4028235e+38
float64 8字节 4.9e-324~1.7976931348623157e+308
3、字符串型
数据类型 大小
string 16字节
可复制代码
package main

import (
	"fmt"
	"math"
	"unsafe"
)

func main() {
  var a int
  var b int8
  var c int16
  var d int32
  var e int64
  var aa uint
  var bb uint8
  var cc uint16
  var dd uint32
  var ee uint64
  fmt.Println("========== 基本数字类型 ==========")

  fmt.Println("===== 1、布尔类型 =====")
  var boolean bool
  fmt.Println("【bool size】", unsafe.Sizeof(boolean), "B")

  fmt.Println("===== 2、数值类型 =====")
  fmt.Println("===== (1)整型描述 =====")
  fmt.Println("【int size】", unsafe.Sizeof(a), "B", "【int floor】", math.MinInt, "【int ceiling】", math.MaxInt)
  fmt.Println("【int8 size】", unsafe.Sizeof(b), "B", "【int8 floor】", math.MinInt8, "【int8 ceiling】", math.MaxInt8)
  fmt.Println("【int16 size】", unsafe.Sizeof(c), "B", "【int16 floor】", math.MinInt16, "【int16 ceiling】", math.MaxInt16)
  fmt.Println("【int32 size】", unsafe.Sizeof(d), "B", "【int32 floor】", math.MinInt32, "【int32 ceiling】", math.MaxInt32)
  // math.MaxInt64是一个常量,而不是int64。
  fmt.Println("【int64 size】", unsafe.Sizeof(e), "B", "【int64 floor】", math.MinInt64, "【int64 ceiling】", int64(math.MaxInt64))

  // math.MaxUint是一个常量,而不是uint。
  fmt.Println("【uint size】", unsafe.Sizeof(aa), "B", "【uint floor】", 0, "【uint ceiling】", uint(math.MaxUint))
  fmt.Println("【uint8 size】", unsafe.Sizeof(bb), "B", "【uint8 floor】", 0, "【uint8 ceiling】", math.MaxUint8)
  fmt.Println("【uint16 size】", unsafe.Sizeof(cc), "B", "【uint16 floor】", 0, "【uint16 ceiling】", math.MaxUint16)
  fmt.Println("【uint32 size】", unsafe.Sizeof(dd), "B", "【uint32 floor】", 0, "【uint32 ceiling】", math.MaxUint32)
  // math.MaxUint64是一个常量,而不是uint64。
  fmt.Println("【uint64 size】", unsafe.Sizeof(ee), "B", "【uint64 floor】", 0, "【uint64 ceiling】", uint64(math.MaxUint64))

  fmt.Println("===== (2)浮点型描述 =====")
  var f32 float32
  var f64 float64
  var complex64 complex64
  var complex128 complex128

  fmt.Println("【float32 size】", unsafe.Sizeof(f32), "B", "【float32 floor】", "1.4e-45", "【float32 ceiling】", float32(math.MaxFloat32))
  fmt.Println("【float64 size】", unsafe.Sizeof(f64), "B", "【float64 floor】", "4.9e-324", "【float64 ceiling】", math.MaxFloat64)
  fmt.Println("【complex64 size】", unsafe.Sizeof(complex64), "B", "【组成】32位实数部分+32位虚数部分")
  fmt.Println("【complex128 size】", unsafe.Sizeof(complex128), "B", "【组成】64位实数部分+64位虚数部分")

  fmt.Println("===== 3、字符串型描述 =====")
  var str string
  fmt.Println("【string size】", unsafe.Sizeof(str), "B")

}

代码结果
GOROOT=C:\Program Files\Go #gosetup
GOPATH=C:\gowork #gosetup
"C:\Program Files\Go\bin\go.exe" build -o C:\Users\ASUS\AppData\Local\Temp\GoLand\___go_build_007_basic_data_type_go.exe C:\gowork\src\007_basic_data_type.go #gosetup
C:\Users\ASUS\AppData\Local\Temp\GoLand\___go_build_007_basic_data_type_go.exe
========== 基本数字类型 ==========
===== 1、布尔类型 =====
【bool size】 1 B
===== 2、数值类型 =====
===== (1)整型描述 =====
【int size】 8 B 【int floor】 -9223372036854775808 【int ceiling】 9223372036854775807
【int8 size】 1 B 【int8 floor】 -128 【int8 ceiling】 127
【int16 size】 2 B 【int16 floor】 -32768 【int16 ceiling】 32767
【int32 size】 4 B 【int32 floor】 -2147483648 【int32 ceiling】 2147483647
【int64 size】 8 B 【int64 floor】 -9223372036854775808 【int64 ceiling】 9223372036854775807
【uint size】 8 B 【uint floor】 0 【uint ceiling】 18446744073709551615
【uint8 size】 1 B 【uint8 floor】 0 【uint8 ceiling】 255
【uint16 size】 2 B 【uint16 floor】 0 【uint16 ceiling】 65535
【uint32 size】 4 B 【uint32 floor】 0 【uint32 ceiling】 4294967295
【uint64 size】 8 B 【uint64 floor】 0 【uint64 ceiling】 18446744073709551615
===== (2)浮点型描述 =====
【float32 size】 4 B 【float32 floor】 1.4e-45 【float32 ceiling】 3.4028235e+38
【float64 size】 8 B 【float64 floor】 4.9e-324 【float64 ceiling】 1.7976931348623157e+308
【complex64 size】 8 B 【组成】32位实数部分+32位虚数部分
【complex128 size】 16 B 【组成】64位实数部分+64位虚数部分
===== 3、字符串型描述 =====
【string size】 16 B

Process finished with the exit code 0

posted @ 2022-09-05 21:37  顺心无忧  阅读(44)  评论(0编辑  收藏  举报