Home > Article > Backend Development > What data types are there in go language?
The data types of the Go language are: 1. Boolean type, the value can only be a constant true or false; 2. Numeric type, supports integers and floating-point numbers, and supports complex numbers; 3. String type , is a character sequence connected by a string of fixed-length characters; 4. Pointer type; 5. Array type; 6. Structured type; 7. Channel type; 8. Function type; 9. Slice type; 10. Interface type; 11. Map type.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
In the Go programming language, data types are used to declare functions and variables.
The emergence of data types is to divide data into data with different memory sizes. When programming, you need to apply for large memory only when you need to use big data, so that you can make full use of the memory.
Go language has the following data types according to categories:
Serial number | Type and description |
---|---|
1 |
Boolean type The value of Boolean type can only be the constant true or false. A simple example: var b bool = true. |
2 |
Number type Integer type int and floating point type float32, float64, Go language supports integer and floating point types Numbers, and supports complex numbers, in which bit operations use two's complement. |
3 |
String type: A string is a sequence of characters connected by a fixed length of characters. Go's strings are concatenated from individual bytes. The bytes of Go language strings use UTF-8 encoding to identify Unicode text. |
4 |
Derived types: Includes:
|
There are rich data types in Go language. In addition to basic integers, floating point types, Boolean types, and strings, there are also arrays, slices, structures, functions, maps, channels, etc.
int8、int16、int32、int64
uint8、uint16、uint32、uint64
uint8 对应 byte 型 int16 对应 C 语言中的 short 型 int64 对应 C 语言中的 long 型
Description | |
---|---|
Signed 8-bit integer (-128 to 127) | |
Signed 16-bit integer type (-32768 to 32767) | |
Signed 32-bit integer type (-2147483648 to 2147483647) | |
Signed 64-bit integer type (-9223372036854775808 to 9223372036854775807) | |
Unsigned 8-bit integer type (0 to 255) | |
Unsigned 16-bit integer (0 to 65535) | |
Unsigned 32-bit integer type (0 to 4294967295) | |
Unsigned 64-bit integer type (0 to 18446744073709551615) |
Description | |
---|---|
On 32-bit operating systems, it is int32, and on 64-bit operating systems, it is int64 | |
On 32-bit operating systems, it is uint32, on 64-bit operating systems, it is uint64 | |
Unsigned integer type, used to store a pointer |
转义符 | 含义 |
---|---|
\r | 回车符 (返回行首) |
\n | 换行符 (直接跳到下一行的同列位置) |
\t | 制表符 |
' | 单引号 |
" | 双引号 |
\ | 反斜杠 |
package main import ( "fmt" ) func main() { // 转义符的使用 fmt.Println("\n# 转义符的使用 str := \"c:\\go\"") }
方法 | 方法说明 |
---|---|
len(str) | 求长度 |
+或fmt.Sprintf | 拼接字符串 |
strings.Split | 分割 |
strings.contains | 判断是否包含 |
strings.HasPrefix,strings.HasSuffix | 前缀/后缀判断 |
strings.Index(),strings.LastIndex() | 子串出现的位置 |
strings.Join(a[]string, sep string) | join操作 |
package main import ( "fmt" "strings" ) // 字符串操作 func main() { // 字符串求长度 s3 := "zhongguojueqi" fmt.Println("\n字符串-求长度: ", len(s3)) // 字符串拼接 s4 := "nihaoshijie" fmt.Println("\n字符串-拼接01: ", s3+s4) s5 := fmt.Sprintf("%s---%s", s3, s4) fmt.Println("\n字符串-拼接02: ", s5) // 字符串分割 s6 := strings.Split(s3, "o") fmt.Println("\n字符串-分割: ", s6) // 字符串包含判断 s7 := strings.Contains(s3, "o") fmt.Println("\n字符串-包含判断01: ", s7) fmt.Println("\n字符串-包含判断02: ", strings.Contains(s3, "o")) // 字符串前缀, 后缀判断 fmt.Println("\n字符串-前缀判断: ", strings.HasPrefix(s3, "zhong")) fmt.Println("\n字符串-后缀判断: ", strings.HasSuffix(s3, "qi")) // 字符串索引查找 fmt.Println("\n字符串-索引查找-第一个字符 o 的索引: ", strings.Index(s3, "o")) fmt.Println("\n字符串-索引查找-最后一个字符 o 的索引: ", strings.LastIndex(s3, "o")) // 字符串-join操作 s8 := []string{"aaa", "bbb", "ccc", "ddd"} fmt.Println("\n字符串-join 操作: ", strings.Join(s8, " + ")) }
func runeDemo01() { // 字符定义 a := '中' // 默认识别为 rune 类型的字符变量 b := "中" // 定义一个字符串 var c byte = 'a' // 定义一个byte类型字符 var d rune = 'a' // 定义一个rune类型字符 fmt.Println(a, b) fmt.Printf("%v,%T\n", c, c) fmt.Printf("%v,%T\n", d, d) }
// 字符串遍历-traversalString package main import ( "fmt" ) func traversalString01() { s := "hello世界" for i := 0; i < len(s); i++ { // 中英文使用 for循环加 len() 方法遍历循环,但遇到中文会有乱码 fmt.Printf("%v(%c) ", s[i], s[i]) } fmt.Println() fmt.Println([]byte(s)) } ----------------------- 104(h) 101(e) 108(l) 108(l) 111(o) 228(ä) 184(¸) 150() 231(ç) 149() 140() -----------------------
package main import ( "fmt" ) // 遍历字符串 traversalString func traversalString02() { s := "hello世界" fmt.Println() for _, r := range s { // 按照 rune 类型遍历 fmt.Printf("%v(%c) ", r, r) } fmt.Println() fmt.Println([]rune(s)) } ----------------------- 104(h) 101(e) 108(l) 108(l) 111(o) 19990(世) 30028(界) -----------------------
1.因为 UTF8 编码下一个中文汉字由 3~4 个字节组成,所以我们不能简单的按照字节去遍历一个包含中文的字符串,否则就会出现上面输出中第一行的结果 2.字符串底层是一个 byte 数组,所以可以和 []byte 类型相互转换 3.字符串是不能修改的 字符串是由 byte 字节组成,所以字符串的长度是 byte 字节的长度 4.rune 类型用来表示 utf8 字符,一个 rune 字符由一个或多个 byte 组成。
func changeString() { s1 := "big" // 强制类型转换 byteS1 := []byte(s1) byteS1[0] = 'p' fmt.Println(string(byteS1)) s2 := "白萝卜" runeS2 := []rune(s2) runeS2[0] = '红' fmt.Println(string(runeS2)) }
T(表达式)
func sqrtDemo() { var a, b = 3, 4 var c int // math.Sqrt()接收的参数是float64类型,需要强制转换 c = int(math.Sqrt(float64(a*a + b*b))) fmt.Println(c) }
计算直角三角形的斜边长时使用 math 包的 Sqrt() 函数,该函数接收的是 float64 类型的参数
而变量 a 和 b 都是 int 类型的,这个时候就需要将 a 和 b 强制类型转换为 float64 类型
The above is the detailed content of What data types are there in go language?. For more information, please follow other related articles on the PHP Chinese website!