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.
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 <h3 id="strong-字符串遍历-方法-rune-类型遍历可以使用-for-range-循环-strong"><strong>7.2.字符串遍历-方法2-rune-类型遍历可以使用 for range 循环</strong></h3><pre class="brush:php;toolbar:false">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!

Go's "strings" package provides rich features to make string operation efficient and simple. 1) Use strings.Contains() to check substrings. 2) strings.Split() can be used to parse data, but it should be used with caution to avoid performance problems. 3) strings.Join() is suitable for formatting strings, but for small datasets, looping = is more efficient. 4) For large strings, it is more efficient to build strings using strings.Builder.

Go uses the "strings" package for string operations. 1) Use strings.Join function to splice strings. 2) Use the strings.Contains function to find substrings. 3) Use the strings.Replace function to replace strings. These functions are efficient and easy to use and are suitable for various string processing tasks.

ThebytespackageinGoisessentialforefficientbyteslicemanipulation,offeringfunctionslikeContains,Index,andReplaceforsearchingandmodifyingbinarydata.Itenhancesperformanceandcodereadability,makingitavitaltoolforhandlingbinarydata,networkprotocols,andfileI

Go uses the "encoding/binary" package for binary encoding and decoding. 1) This package provides binary.Write and binary.Read functions for writing and reading data. 2) Pay attention to choosing the correct endian (such as BigEndian or LittleEndian). 3) Data alignment and error handling are also key to ensure the correctness and performance of the data.

The"bytes"packageinGooffersefficientfunctionsformanipulatingbyteslices.1)Usebytes.Joinforconcatenatingslices,2)bytes.Bufferforincrementalwriting,3)bytes.Indexorbytes.IndexByteforsearching,4)bytes.Readerforreadinginchunks,and5)bytes.SplitNor

Theencoding/binarypackageinGoiseffectiveforoptimizingbinaryoperationsduetoitssupportforendiannessandefficientdatahandling.Toenhanceperformance:1)Usebinary.NativeEndianfornativeendiannesstoavoidbyteswapping.2)BatchReadandWriteoperationstoreduceI/Oover

Go's bytes package is mainly used to efficiently process byte slices. 1) Using bytes.Buffer can efficiently perform string splicing to avoid unnecessary memory allocation. 2) The bytes.Equal function is used to quickly compare byte slices. 3) The bytes.Index, bytes.Split and bytes.ReplaceAll functions can be used to search and manipulate byte slices, but performance issues need to be paid attention to.

The byte package provides a variety of functions to efficiently process byte slices. 1) Use bytes.Contains to check the byte sequence. 2) Use bytes.Split to split byte slices. 3) Replace the byte sequence bytes.Replace. 4) Use bytes.Join to connect multiple byte slices. 5) Use bytes.Buffer to build data. 6) Combined bytes.Map for error processing and data verification.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
