What data types are there in go language?
Dec 20, 2022 pm 12:00 PMThe 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.
Go language data types
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.
1. Integer
- ##Integer
1.1. Integer type is divided into two categories
- Divided according to the occupied memory length
int8、int16、int32、int64
- Divided according to whether there is a positive or negative sign - unsigned integer
uint8、uint16、uint32、uint64
- With C Language comparison
uint8 对应 byte 型 int16 对应 C 语言中的 short 型 int64 对应 C 语言中的 long 型
1.2. Plastic description
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) |
1.3. Special integer type
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 | 制表符 |
' | 单引号 |
" | 双引号 |
\ | 反斜杠 |
5.3.字符串转义-实例演示
package main import ( "fmt" ) func main() { // 转义符的使用 fmt.Println("\n# 转义符的使用 str := \"c:\\go\"") }
5.4.字符串操作
方法 | 方法说明 |
---|---|
len(str) | 求长度 |
+或fmt.Sprintf | 拼接字符串 |
strings.Split | 分割 |
strings.contains | 判断是否包含 |
strings.HasPrefix,strings.HasSuffix | 前缀/后缀判断 |
strings.Index(),strings.LastIndex() | 子串出现的位置 |
strings.Join(a[]string, sep string) | join操作 |
5.5.字符串操作-实例演示
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, " + ")) }
6.字符
6.1.定义一个字符变量
- 组成字符串的元素叫做 字符,使用单引号进行定义字符类型变量,字符串使用双引号定义
- 可以通过遍历或者单个获取字符串元素获得字符
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) }
6.2.字符类型有两种
6.2.1.uint8-类型字符
- go 语言中一般的英文数字字符使用 ASCII 码的一个字符,占据 8 位 bit 的内存空间,也就是常用的 byte 型
6.2.2.rune-类型字符
- go 语言中处理中文日文或者其他复合字符时,需要用到 rune 类型,rune 类型实际是一个 int32,代表一个 UTF-8 字符(Unicode编码)
7.字符串拓展
7.1.字符串遍历-方法1-使用循环依次取出字符串中的元素
// 字符串遍历-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() -----------------------
7.2.字符串遍历-方法2-rune-类型遍历可以使用 for range 循环
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 组成。
7.3.字符串修改
- 字符串是固定值无法修改,如果要修改字符串,需要先将其转换成 []rune 或 []byte,完成后再转换为 string 类型。无论哪种转换,都会重新分配内存,并复制字节数组
func changeString() { s1 := "big" // 强制类型转换 byteS1 := []byte(s1) byteS1[0] = 'p' fmt.Println(string(byteS1)) s2 := "白萝卜" runeS2 := []rune(s2) runeS2[0] = '红' fmt.Println(string(runeS2)) }
8.类型转换
- Go语言中只有强制类型转换,没有隐式类型转换。该语法只能在两个类型之间支持相互转换的时候使用
8.1.类型转换-语法
T(表达式)
- 其中,T 表示要转换的类型,表达式包括变量、复杂算子和函数返回值等
8.1.类型转换-实例演示
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to safely read and write files using Golang?

How to configure connection pool for Golang database connection?

Similarities and Differences between Golang and C++

How steep is the learning curve of golang framework architecture?

How to generate random elements from list in Golang?

Comparison of advantages and disadvantages of golang framework

What are the best practices for error handling in Golang framework?

What are the advantages of golang framework?
