这是我参与2022首次更文挑战的第14天,活动详情查看:2022首次更文挑战
数据类型指定了一个有效的go变量可以持有的数据的类型。在go语言中,类型被分成了以下四类:
Basic type基础类型:Numbers, strings, and booleans在这个分类下。Aggregate type聚合类型:Array, structs。Reference type引用类型:Pointers, slices, maps, functions, channelsInterface type接口类型
本文我们会讨论一个go语言中的基础数据类型。基础数据类型分成以下三个子类:
- Numbers
- Booleans
- Strings
Numbers
在go语言中,数字分成了三个子类:
Integers整数类型:在go语言中,在下面的表格中展示的,有符号和无符号的四种不同尺寸的整数都是可用的。有符号整数由int来代表,无符号整数由uint来代表 |DataType|Description| |----|----| |int8|8-bit有符号整数| |int16|16-bit有符号整数| |int32|32-bit有符号整数| |int64|64-bit有符号整数| |uint8|8-bit无符号整数| |uint16|16-bit无符号整数| |uint32|32-bit无符号整数| |uint64|64-bit无符号整数| |int|有符号整数,32或64位| |uint|无符号整数,32或64位| |rune|int32的别称,用来区分出字符值| |byte|int8的别称,用来区分出byte的值| |uintptr|无符号整数类型。宽度未定义,可以用来接收任意bit模式的指针的值|
例子:
// Go program to illustrate
// the use of integers
package main
import "fmt"
func main() {
// Using 8-bit unsigned int
var X uint8 = 225
fmt.Println(X, X-3)
// Using 16-bit signed int
var Y int16 = 32767
fmt.Println(Y+2, Y-2)
}
输出:
225 222
-32767 32765
Floating-Point Numbers浮点数: 在go语言中,浮点数分成以下两种: |DataType|Description| |float32|32bit的IEEE 754的浮点数| |float64|64bit的IEEE 754的浮点数|
例子
// Go program to illustrate
// the use of floating-point
// numbers
package main
import "fmt"
func main() {
a := 20.45
b := 34.89
// Subtraction of two
// floating-point number
c := b-a
// Display the result
fmt.Printf("Result is: %f", c)
// Display the type of c variable
fmt.Printf("\nThe type of c is : %T", c)
}
输出
Result is: 14.440000
The type of c is : float64
Complex Numbers复数: 复数分成两种。float32和float64也是这些复数的一部分。内置函数创建一个复数,包含了实数和虚数的部分。 |DataType|Description| |complex64|包含float32的实数和虚数部分| |complex128|包含float64的实数和虚数部分|
例子
// Go program to illustrate
// the use of complex numbers
package main
import "fmt"
func main() {
var a complex128 = complex(6, 2)
var b complex64 = complex(9, 2)
fmt.Println(a)
fmt.Println(b)
// Display the type
fmt.Printf("The type of a is %T and "+
"the type of b is %T", a, b)
}
输出
(6+2i)
(9+2i)
The type of a is complex128 and the type of b is complex64
Booleans
布尔类型,只代表true或false的1bit。布尔类型的值无法强转或者暗转成其他类型。 例子
// Go program to illustrate
// the use of booleans
package main
import "fmt"
func main() {
// variables
str1 := "GeeksforGeeks"
str2:= "geeksForgeeks"
str3:= "GeeksforGeeks"
result1:= str1 == str2
result2:= str1 == str3
// Display the result
fmt.Println( result1)
fmt.Println( result2)
// Display the type of
// result1 and result2
fmt.Printf("The type of result1 is %T and "+
"the type of result2 is %T",
result1, result2)
}
输出
false
true
The type of result1 is bool and the type of result2 is bool
Strings
string类型代表unicode编码的序列。换一种说法,我们可以说字符串是不可变的位的序列,意味着一个字符串一旦创建,你就无法修改。一个字符串可能包含任意的数据,包括人们理解的空白表单零值。 例子
// Go program to illustrate
// the use of strings
package main
import "fmt"
func main() {
// str variable which stores strings
str := "GeeksforGeeks"
// Display the length of the string
fmt.Printf("Length of the string is:%d",
len(str))
// Display the string
fmt.Printf("\nString is: %s", str)
// Display the type of str variable
fmt.Printf("\nType of str is: %T", str)
}
输出
Length of the string is:13
String is: GeeksforGeeks
Type of str is: string