类型

预先声明的类型(或内置类型)

​ 合计27种。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
布尔型共1种】:
bool

数值型共16种】:
uint8 uint16 uint32 uint64 uint
int6  int16  int32  int64  int
byteuint8的别名runeint32的别名float32      float64
complex64    complex128

字符串型共1种string

数组型共1种[N]TN为元素个数T为元素类型
切片型共1种[]TT为元素类型
结构体型共1种】:
struct{...fields fieldsType}

指针型共1种】:
*T

指针地址型/指针整数型共1种】:
uintptr

函数型共1种】:
func(...params paramsType) ...returns returnsType 

接口型共1种】:
interface{...methods}

map型共1种】:
map[keyType]valueType

chan型共1种】:
chan TT为通道中所传递的值类型

Go的内置数据类型相关信息

序号数据类型名称别名默认值占用字节数数据最小值数据最大值备注链接
1bool布尔型false1只有true和false两种值
2byte字节型‘\x00’(使用%q)0(使用%d)1‘\x00’(使用%q)0(使用%d)‘\x7f’(使用%q)127(使用%d)是uint8的别名
3complex64复数型(0+0i)8
4complex128复数型(0+0i)16
5float32浮点型041.401298464324817070923729583289916131280e-45 最小正非零值(使用%.39e保留39位小数)3.40282346638528859811704183484516925440e+38 (使用%.38e保留38位小数)
6float64浮点型084.94065645841246544176568792868221372365059803e-324 最小正非零值(使用%.44e保留44位小数)1.797693134862315708145274237317043567980706e+308 (使用%.42e保留42位小数)
7int8有符号整型01-128127
8int16有符号整型02-3276832767
9int32有符号整型rune04-21474836482147483647
10int64有符号整型08-92233720368547758089223372036854775807(>922亿亿)
11int有符号整型08-92233720368547758089223372036854775807(>922亿亿)请注意:这里给出的是64位系统的情况!
12uint8无符号整型byte010255
13uint16无符号整型02065535
14uint32无符号整型0404294967295
15uint64无符号整型08018446744073709551615
16uint无符号整型08018446744073709551615
17rune符文型‘\x00’(使用%q)0(使用%d)4‘\x00’(使用%q)0(使用%d)是int32的别名,而非uint32的别名
18uintptr指针整数型0uintptr 是一个整数类型,它足够大,可以容纳任何指针的比特模式
19string字符串型""
20[n]T数组空数组
21[]T切片nil
22map[K]V映射nil
23struct{…}结构体各自字段的零值
24chan T通道nil
25*T指针nil
26interface接口nil
27error错误类型

获取内置类型的默认值

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main

import "fmt"

func main() {
	fmt.Println("default value:")
	var b bool
	var ui8 uint8
	var ui16 uint16
	var ui32 uint32
	var ui64 uint64
	var ui uint
	var i8 int8
	var i16 int16
	var i32 int32
	var i64 int64
	var i int
	var s string
	var ai [3]int
	var as [3]string
	var ab [3]bool
	var sli []int
	var sls []string
	var slb []bool
	type St struct {
		a int
		b string
		c *int
		d chan int
	}
	var st St
	var pti *int
	var pts *string
	var ptb *bool
	var uptr uintptr
	var fti func(int, int) int
	var fts func(string, string) string
	var ftb func(bool, bool) bool
	type ITF interface {
		M1(int) int
		M2(string) string
		M3(bool) bool
	}
	var itf ITF
	var mii map[int]int
	var mss map[string]string
	var mbb map[bool]bool
	var chi chan int
	var chs chan string
	var chb chan bool

	fmt.Printf("bool -> %t\n", b)
	fmt.Printf("uint8 -> %d\n", ui8)
	fmt.Printf("uint16 -> %d\n", ui16)
	fmt.Printf("uint32 -> %d\n", ui32)
	fmt.Printf("uint64 -> %d\n", ui64)
	fmt.Printf("uint -> %d\n", ui)
	fmt.Printf("int8 -> %d\n", i8)
	fmt.Printf("int16 -> %d\n", i16)
	fmt.Printf("int32 -> %d\n", i32)
	fmt.Printf("int64 -> %d\n", i64)
	fmt.Printf("int -> %d\n", i)
	fmt.Printf("string -> %q\n", s)
	fmt.Printf("array [3]int -> %#v\n", ai)
	fmt.Printf("array [3]string -> %#v\n", as)
	fmt.Printf("array [3]bool -> %#v\n", ab)
	fmt.Printf("slice []int -> %#v\n", sli)
	fmt.Printf("slice []int -> %v\n", sli)
	if sli == nil {
		fmt.Println("sli is nil.")
	}
	fmt.Printf("slice []string -> %#v\n", sls)
	fmt.Printf("slice []string -> %v\n", sls)
	if sls == nil {
		fmt.Println("sls is nil.")
	}
	fmt.Printf("slice []bool -> %#v\n", slb)
	fmt.Printf("slice []bool -> %v\n", slb)
	if slb == nil {
		fmt.Println("slb is nil.")
	}
	fmt.Printf("struct st -> %#v\n", st)
	fmt.Printf("struct st -> %v\n", st)
	fmt.Printf("point *int -> %v\n", pti)
	if pti == nil {
		fmt.Println("pti is nil.")
	}
	fmt.Printf("point *string -> %v\n", pts)
	if pts == nil {
		fmt.Println("pts is nil.")
	}
	fmt.Printf("point *bool -> %v\n", ptb)
	if ptb == nil {
		fmt.Println("ptb is nil.")
	}
	fmt.Printf("uintptr -> %v\n", uptr)
	fmt.Printf("func(int,int) int -> %v\n", fti)
	if fti == nil {
		fmt.Println("fti is nil.")
	}
	fmt.Printf("func(string,string) string -> %v\n", fts)
	if fts == nil {
		fmt.Println("fts is nil.")
	}

	fmt.Printf("func(bool,bool) bool -> %v\n", ftb)
	if ftb == nil {
		fmt.Println("ftb is nil.")
	}

	fmt.Printf("interface -> %v\n", itf)
	if itf == nil {
		fmt.Println("itf is nil.")
	}

	fmt.Printf("map[int]int -> %#v\n", mii)
	fmt.Printf("map[int]int -> %v\n", mii)
	if mii == nil {
		fmt.Println("mii is nil.")
	}
	fmt.Printf("map[string]string -> %#v\n", mss)
	fmt.Printf("map[string]string -> %v\n", mss)
	if mss == nil {
		fmt.Println("mss is nil.")
	}
	fmt.Printf("map[bool]bool -> %#v\n", mbb)
	fmt.Printf("map[bool]bool -> %v\n", mbb)
	if mbb == nil {
		fmt.Println("mbb is nil.")
	}

	fmt.Printf("chan int -> %#v\n", chi)
	fmt.Printf("chan int -> %v\n", chi)
	if chi == nil {
		fmt.Println("chi is nil.")
	}
	fmt.Printf("chan string -> %#v\n", chs)
	fmt.Printf("chan string -> %v\n", chs)
	if chs == nil {
		fmt.Println("chs is nil.")
	}
	fmt.Printf("chan bool -> %#v\n", chb)
	fmt.Printf("chan bool -> %v\n", chb)
	if chb == nil {
		fmt.Println("chb is nil.")
	}
}
//default value:
//bool -> false
//uint8 -> 0
//uint16 -> 0
//uint32 -> 0
//uint64 -> 0
//uint -> 0
//int8 -> 0
//int16 -> 0
//int32 -> 0
//int64 -> 0
//int -> 0
//string -> ""
//array [3]int -> [3]int{0, 0, 0}
//array [3]string -> [3]string{"", "", ""}
//array [3]bool -> [3]bool{false, false, false}
//slice []int -> []int(nil)
//slice []int -> []
//sli is nil.
//slice []string -> []string(nil)
//slice []string -> []
//sls is nil.
//slice []bool -> []bool(nil)
//slice []bool -> []
//slb is nil.
//struct st -> main.St{a:0, b:"", c:(*int)(nil), d:(chan int)(nil)}
//struct st -> {0  <nil> <nil>}
//point *int -> <nil>
//pti is nil.
//point *string -> <nil>
//pts is nil.
//point *bool -> <nil>
//ptb is nil.
//uintptr -> 0
//func(int,int) int -> <nil>
//fti is nil.
//func(string,string) string -> <nil>
//fts is nil.
//func(bool,bool) bool -> <nil>
//ftb is nil.
//interface -> <nil>
//itf is nil.
//map[int]int -> map[int]int(nil)
//map[int]int -> map[]
//mii is nil.
//map[string]string -> map[string]string(nil)
//map[string]string -> map[]
//mss is nil.
//map[bool]bool -> map[bool]bool(nil)
//map[bool]bool -> map[]
//mbb is nil.
//chan int -> (chan int)(nil)
//chan int -> <nil>
//chi is nil.
//chan string -> (chan string)(nil)
//chan string -> <nil>
//chs is nil.
//chan bool -> (chan bool)(nil)
//chan bool -> <nil>
//chb is nil.
最后修改 October 10, 2024: 更新 (a4b8f85)