默认值
Type |
默认值 |
Interger |
0 |
Float |
0 |
Complex Number |
实数部分:0 虚数部分:0 |
Byte |
0 |
Rune |
0 |
String |
"" |
Bool |
false |
Array |
元素的默认值 |
Slice |
nil |
Map |
nil |
Channel |
nil |
Interface |
nil |
Struct |
字段类型的默认值 |
Pointer |
nil |
Function |
nil |
验证
package main
import (
"fmt"
)
func main() {
var a int
fmt.Println("int 默认值: ", a)
var b uint
fmt.Println("uint 默认值: ", b)
var c float64
fmt.Println("float 默认值: ", c)
var d complex64
fmt.Println("complex 默认值: ", d)
var e byte
fmt.Println("byte 默认值: ", e)
var f rune
fmt.Println("rune 默认值: ", f)
var g string
fmt.Printf("string 默认值: %q \n", g)
var h bool
fmt.Println("bool 默认值: ", h)
var i [3]bool
fmt.Println("array 默认值: ", i)
var j []int
fmt.Println("slice is nil: ", j == nil, " 默认值: ", j)
var k map[int]bool
fmt.Println("map is nil: ", k == nil, " 默认值: ", k)
var l chan int
fmt.Println("channel 默认值: ", l)
var m interface{}
fmt.Println("interface 默认值: ", m)
type Staff struct {
name string
age int
sex bool
}
var n = Staff{}
fmt.Println("struct 默认值: ", n)
var o func()
fmt.Println("function 默认值: ", o)
var p *int
fmt.Println("pointer 默认值: ", p)
}
结果
各类型的最大/最小值
//无符号整型, 默认值都是 0
var u8 uint8 //0
var u16 uint16 //0
var u32 uint32 //0
var u64 uint64 //0
fmt.Println("无符号整型:")
fmt.Printf("default -> u8: %d, u16: %d, u32: %d, u64: %d\n", u8, u16, u32, u64)
//无符号整型,最大值
u8 = math.MaxUint8 // 1<<8 - 1 //255
u16 = math.MaxUint16 // 1<<16 - 1 //65535
u32 = math.MaxUint32 // 1<<32 - 1 //4294967295
u64 = math.MaxUint64 // 1<<64 - 1 //18446744073709551615
fmt.Printf("max -> u8: %d, u16: %d, u32: %d, u64: %d\n", u8, u16, u32, u64)
//整型, 默认值都是 0
var i8 int8
var i16 int16
var i32 int32
var i64 int64
//int 32位系统取值范围为 int32, 64位系统取值范围为 int64 ,两种系统取值相同,但类型不同
var i int
//i = i32 //报错,编译不通过,类型不同
//i = i64 //报错,编译不通过,类型不同
fmt.Println("整型:")
fmt.Printf("default -> i8: %d, i16: %d, i32: %d, i64: %d, i: %d\n", i8, i16, i32, i64, i)
//整型, 最小值
i8 = math.MinInt8 // -1 << 7
i16 = math.MinInt16 // -1 << 15
i32 = math.MinInt32 // -1 << 31
i64 = math.MinInt64 // -1 << 63
i = -1 << 63
fmt.Printf("min -> i8: %d, i16: %d, i32: %d, i64: %d, i: %d\n", i8, i16, i32, i64, i)
//整型, 最大值
i8 = math.MaxInt8 // 1<<7 - 1
i16 = math.MaxInt16 // 1<<15 - 1
i32 = math.MaxInt32 // 1<<31 - 1
i64 = math.MaxInt64 // 1<<63 - 1
i = 1<<63 - 1
fmt.Printf("max -> i8: %d, i16: %d, i32: %d, i64: %d, i: %d\n", i8, i16, i32, i64, i)
//浮点型 默认值: 0.000000
//float32 精度6位小数
var f32 float32
//float64 精度15位小数 优先使用此类型
var f64 float64
fmt.Println("浮点型:")
fmt.Printf("default: f32: %f, f64: %f\n", f32, f64)
//浮点型 最小值
f32 = math.SmallestNonzeroFloat32 // 1 / 2 **(127 - 1 + 23)
f64 = math.SmallestNonzeroFloat64 // 1 / 2 ** (1023 -1 + 52)
fmt.Printf("min: f32: %v, f64: %v\n", f32, f64)
//浮点型 最大值
f32 = math.MaxFloat32 // 2**127 * (2**24 - 1) / 2**23
f64 = math.MaxFloat64 // 2**1023 * (2**53 -1) / 2**52
fmt.Printf("max: f32: %v, f64: %v\n", f32, f64)
f32 = 1.12345678
f64 = 1.12345678901234567
fmt.Printf("f32: %v, f64: %v\n", f32, f64) // 末位四舍五入:f32: 1.1234568, f64: 1.1234567880123457
网友评论