package main
import (
"fmt"
"unsafe"
)
func main() {
//有符号整数类型:int8-int16-int32-int64
//占用字节空间 1 2 4 8
var n1,n2 int8 = -128,127
fmt.Println(n1,n2) //打印int8类型取值范围
fmt.Printf("取值范围的类型是:%T",n1) //打印类型
fmt.Println()
fmt.Println("打印函数类型占用字节数:",unsafe.Sizeof(n1)) //打印函数类型占用字节数
fmt.Println("int8取值范围是:-128-127,超出这个范围就会报错:constant 128 overflows int8")
fmt.Println()
fmt.Println("-------------------------------------------------------------------")
//int16
var(
n3,n4 int16 = -32768,32767
)
fmt.Println("int16的取值范围是:",n3,"-",n4)
fmt.Printf("取值范围的类型是:%T",n3) //打印类型
fmt.Println()
fmt.Println("打印函数类型占用字节数是:",unsafe.Sizeof(n3),"个字节")//打印函数类型占用字节数
fmt.Println("-------------------------------------------------------------------")
//int32
var(
n5,n6 int32 = -2147483648,2147483647
)
fmt.Println("int32的取值范围是:",n5,"-",n6)
fmt.Printf("取值范围的类型是:%T",n5)
fmt.Println()
fmt.Println("打印函数类型占用的字节数是:",unsafe.Sizeof(n5),"个字节")
fmt.Println("-------------------------------------------------------------------")
//int64
var(
n7,n8 int64 = -9223372036854775808,9223372036854775807
)
fmt.Println("int64的取值范围是:",n7,"-",n8)
fmt.Printf("取值范围的类型是:%T",n7)
fmt.Println()
fmt.Println("打印函数类型占用的字节数是:",unsafe.Sizeof(n7),"个字节")
fmt.Println("-------------------------------------------------------------------")
//系统默认使用的是int类型
var (
test,test1 = -9223372036854775808,9223372036854775807
)
fmt.Printf("系统默认使用的自动类型推断为: %T %T",test,test1)
fmt.Println()
fmt.Println("取值范围跟int64一样",test,"-",test1)
fmt.Println("-------------------------------------------------------------------")
//无符号整数类型:uint8-uint16-uint32-uint64
//占用字节空间 1 2 4 8
//uint8
var(
u1,u2 uint8 = 0,255
)
fmt.Println("uint8的取值范围是:",u1,"-",u2)
fmt.Printf("取值的类型是:%T ",u1)
fmt.Println()
fmt.Println("打印函数类型占用字节数是:",unsafe.Sizeof(u1),"个字节")
fmt.Println("-------------------------------------------------------------------")
//uint16
var(
u3,u4 uint16 = 0,65535
)
fmt.Println("uint16的取值范围是:",u3,"-",u4)
fmt.Printf("取值的类型是:%T ",u3)
fmt.Println()
fmt.Println("打印函数类型占用字节数是:",unsafe.Sizeof(u3),"个字节")
fmt.Println("-------------------------------------------------------------------")
//uint32
var(
u5,u6 uint32 = 0,4294967295
)
fmt.Println("uint32的取值范围是:",u5,"-",u6)
fmt.Printf("取值的类型是:%T ",u5)
fmt.Println()
fmt.Println("打印函数类型占用字节数是:",unsafe.Sizeof(u5),"个字节")
fmt.Println("-------------------------------------------------------------------")
//uint64
var(
u7,u8 uint64 = 0,18446744073709551615
)
fmt.Println("uint64的取值范围是:",u7,"-",u8)
fmt.Printf("取值的类型是:%T ",u7)
fmt.Println()
fmt.Println("打印函数类型占用字节数是:",unsafe.Sizeof(u7),"个字节")
fmt.Println("-------------------------------------------------------------------")
//浮点类型:float32和float64 简单来说就是小数
// 占字节 4 8
var(
f1 float32 = 3.14
f2 float32 = -3.14
f3 float32 = 314e-2
f4 float32 = 314E-2
//浮点数中会出现精度丢失的情况,通常情况下,建议使用:float64
f5 float32 = 256.00000000066
f6 float64 = 256.00000000066
//系统默认使用的也是float64
ftest = 3.1400000064
)
fmt.Println("可以表示正浮点数:",f1,"|","也可以表示负浮点数:",f2)
fmt.Println("浮点数不仅可以用十进制表示,还可以使用科学计数法表示:",f3,f4,"并且科学计数法并不区分大小写")
fmt.Println("float32:",f5,"|","float64:",f6,"为了避免精度丢失还是建议使用float64")
fmt.Printf("系统使用的默认浮点数类型:%T", ftest)
}
对应的运行结果
GOROOT=C:\Users\jingxie\go\go1.16.6 #gosetup
GOPATH=C:\Users\jingxie\go\go1.16.6;C:\Users\jingxie\go #gosetup
C:\Users\jingxie\go\go1.16.6\bin\go.exe build -o C:\Users\jingxie\AppData\Local\Temp\___go_build_golang__1_.exe "C:\Users\jingxie\go\src\golang\5.int and float.go" #gosetup
C:\Users\jingxie\AppData\Local\Temp\___go_build_golang__1_.exe #gosetup
-128 127
取值范围的类型是:int8
打印函数类型占用字节数: 1
int8取值范围是:-128-127,超出这个范围就会报错:constant 128 overflows int8
-------------------------------------------------------------------
int16的取值范围是: -32768 - 32767
取值范围的类型是:int16
打印函数类型占用字节数是: 2 个字节
-------------------------------------------------------------------
int32的取值范围是: -2147483648 - 2147483647
取值范围的类型是:int32
打印函数类型占用的字节数是: 4 个字节
-------------------------------------------------------------------
int64的取值范围是: -9223372036854775808 - 9223372036854775807
取值范围的类型是:int64
打印函数类型占用的字节数是: 8 个字节
-------------------------------------------------------------------
系统默认使用的自动类型推断为: int int
取值范围跟int64一样 -9223372036854775808 - 9223372036854775807
-------------------------------------------------------------------
uint8的取值范围是: 0 - 255
取值的类型是:uint8
打印函数类型占用字节数是: 1 个字节
-------------------------------------------------------------------
uint16的取值范围是: 0 - 65535
取值的类型是:uint16
打印函数类型占用字节数是: 2 个字节
-------------------------------------------------------------------
uint32的取值范围是: 0 - 4294967295
取值的类型是:uint32
打印函数类型占用字节数是: 4 个字节
-------------------------------------------------------------------
uint64的取值范围是: 0 - 18446744073709551615
取值的类型是:uint64
打印函数类型占用字节数是: 8 个字节
-------------------------------------------------------------------
可以表示正浮点数: 3.14 | 也可以表示负浮点数: -3.14
浮点数不仅可以用十进制表示,还可以使用科学计数法表示: 3.14 3.14 并且科学计数法并不区分大小写
float32: 256 | float64: 256.00000000066 为了避免精度丢失还是建议使用float64
系统使用的默认浮点数类型:float64
进程 已完成,退出代码为 0
总结:
//基础结构
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("int类型有符号的取值范围:")
//fmt.Println("int:", math.MinInt, "~", math.MaxInt) 报错,没有 math.MinInt math.MaxInt
fmt.Println("int8:", math.MinInt8, "~", math.MaxInt8)
fmt.Println("int16:", math.MinInt16, "~", math.MaxInt16)
fmt.Println("int32:", math.MinInt32, "~", math.MaxInt32)
fmt.Println("int64:", math.MinInt64, "~", math.MaxInt64)
fmt.Println("------------------------------------------")
fmt.Println("int类型无符号的取值范围:")
fmt.Println("uint8:", "0","~", math.MaxUint8)
fmt.Println("uint16:","0","~", math.MaxUint16)
fmt.Println("uint32:","0","~", math.MaxUint32)
fmt.Println("uint64:","0","~","18446744073709551615")
//fmt.Println("uint64:","0","~", math.MaxUint64) 这样打印不行 因为math.MaxUint64是一个常量,而不是uint64
//num := math.MaxUint64
//fmt.Printf("%d", uint64(num))
fmt.Println()
fmt.Println("--------------------------------------------")
fmt.Println("float浮点数类型取值范围:")
fmt.Println("float32的最小值为:",math.SmallestNonzeroFloat32,"|"," 最大值为:", math.MaxFloat32)
fmt.Println("float64的最小值为:",math.SmallestNonzeroFloat64,"|"," 最大值为:", math.MaxFloat64)
fmt.Println("--------------------------------------------")
}
输出结果:
GOROOT=C:\Users\jingxie\go\go1.16.6 #gosetup
GOPATH=C:\Users\jingxie\go\go1.16.6;C:\Users\jingxie\go #gosetup
C:\Users\jingxie\go\go1.16.6\bin\go.exe build -o C:\Users\jingxie\AppData\Local\Temp\___go_build_test_go.exe C:\Users\jingxie\go\src\golang\test.go #gosetup
C:\Users\jingxie\AppData\Local\Temp\___go_build_test_go.exe #gosetup
int类型有符号的取值范围:
int8: -128 ~ 127
int16: -32768 ~ 32767
int32: -2147483648 ~ 2147483647
int64: -9223372036854775808 ~ 9223372036854775807
------------------------------------------
int类型无符号的取值范围:
uint8: 0 ~ 255
uint16: 0 ~ 65535
uint32: 0 ~ 4294967295
uint64: 0 ~ 18446744073709551615
--------------------------------------------
float浮点数类型取值范围:
float32的最小值为: 1.401298464324817e-45 | 最大值为: 3.4028234663852886e+38
float64的最小值为: 5e-324 | 最大值为: 1.7976931348623157e+308
--------------------------------------------
进程 已完成,退出代码为 0