Home  >  Article  >  Backend Development  >  What data types are there in go language?

What data types are there in go language?

青灯夜游
青灯夜游Original
2022-12-20 12:00:2511622browse

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.

What data types are there in go language?

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:
  • (a) Pointer type (Pointer)

  • (b) Array type

  • (c) Structural type (struct)

  • (d) Channel type

  • (e) Function type

  • (f) Slice type

  • (g) Interface type (interface)

  • (h) Map type

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

TypeDescriptionint8Signed 8-bit integer (-128 to 127)int16Signed 16-bit integer type (-32768 to 32767)int32Signed 32-bit integer type (-2147483648 to 2147483647)int64Signed 64-bit integer type (-9223372036854775808 to 9223372036854775807)uint8Unsigned 8-bit integer type (0 to 255)uint16Unsigned 16-bit integer (0 to 65535)uint32Unsigned 32-bit integer type (0 to 4294967295)uint64Unsigned 64-bit integer type (0 to 18446744073709551615)

1.3. Special integer type

##Typeintuintuintptr

1.4.注意

1.在使用 int 和 uint 类型时,不能假定它是 32 位或 64 位的整型,而是考虑 int 和 uint 可能在不同平台上的差异
2.获取对象的长度的内建 len() 函数返回的长度可以根据不同平台的字节长度进行变化
3.实际使用中,切片或 map 的元素数量等都可以用 int 来表示
4.在涉及到二进制传输,读写文件的结构描述时,为了保持文件的结构不会受到不同编译目标平台字节长度的影响,不要使用 int 和 uint

1.5.数字字面量语法

  • Number literals syntax

  • Go1.13 版本之后引入了 数字字面量语法,这样便于开发者以二进制、八进制或十六进制浮点数的格式定义数字

  • 内存地址是以十六进制数进行保存的

v := 0b00101101, 代表二进制的 101101,相当于十进制的 45
v := 0o377,代表八进制的 377,相当于十进制的 255
v := 0x1p-2,代表十六进制的 1 除以 2²,也就是 0.25
  • 允许用下划线 _ 来分隔数字
v := 123_456 表示 v 的值等于 123456。
  • 可以借助 fmt 函数来将一个整数以不同进制形式展示
package main

import "fmt"

func main() {
    // 定义十进制数
    var a int = 10
    fmt.Printf("%d \n", a) // 占位符 %d 表示使用十进制显示为 10
    fmt.Printf("%b \n", a) // 占位符 %b 表示使用二进制显示为 1010

    // 定义八进制数,需要以 0 开头
    var b int = 077
    fmt.Printf("%o \n", b) // 占位符 %o 表示使用八进制显示为 77

    // 定义十六进制数,需要以 0x 开头
    var c int = 0xff
    fmt.Printf("%x \n", c) // 占位符 %x 表示使用十六进制显示为 ff,小写字母显示
    fmt.Printf("%X \n", c) // 占位符 %X 表示使用十六进制显示为 FF,大写字母显示
}

2.浮点型

  • Go 语言支持两种浮点型数:float32 和 float64 ,这两种浮点型数据格式遵循 IEEE 754 标准
float32 的浮点数的最大范围约为 3.4e38,可以使用常量定义:math.MaxFloat32
float64 的浮点数的最大范围约为 1.8e308,可以使用一个常量定义:math.MaxFloat64
打印浮点数时,可以使用 fmt 包配合 %f
  • 实例演示:
package main
import (
    "fmt"
    "math"
)
func main() {
    fmt.Printf("%f\n", math.Pi)
    fmt.Printf("%.2f\n", math.Pi)
}

3.复数

  • complex64 和 complex128
  • 复数有实部和虚部,complex64 的实部和虚部为 32 位,complex128 的实部和虚部为 64 位
var c1 complex64
c1 = 1 + 2i
var c2 complex128
c2 = 2 + 3i
fmt.Println(c1)
fmt.Println(c2)

4.布尔值

  • Go 语言中以 bool 类型进行声明布尔型数据
1.布尔型数据只有 true 和 false 两个值,布尔类型变量的默认值为 false。
2.Go 语言中不允许将整型强制转换为布尔型.
3.布尔型无法参与数值运算,也无法与其他类型进行转换

5.字符串

  • Go 语言中的字符串以原生数据类型出现,使用字符串就像使用其他原生数据类型(int、bool、float32、float64 等)一样
  • Go 语言字符串使用 UTF-8 编码,可以在 Go 语言的源码中直接添加非 ASCII 码字符

5.1.定义一个字符串类型的变量

  • 使用 双引号 定义 单行字符串 变量

  • 使用 反引号 定义 多行字符串 变量

  • 使用 单引号 定义 单个字符 变量

  • 实例演示

package main

import "fmt"

func main() {
    // 使用 双引号 定义 单行字符串 变量
    s1 := "hello" //定义英文字符串
    s2 := "你好"    // 可以识别非 ASCII 码,默认支持 utf-8 编码

    // 使用 反引号 定义 多行字符串 变量
    s3 := `
11111
22222   // 多行字符串里的双斜杠也表示注释
33333
`

    // 使用 单引号 定义 单个字符 变量
    s4 := 'h'

    fmt.Println("\n单行字符串01", s1)
    fmt.Println("\n单行字符串02", s2)
    fmt.Println("\n多行字符串", s3)
    fmt.Println("\n字符定义", s4)
}

5.2.字符串转义

  • Go 语言的字符串常见转义符包含回车、换行、单双引号、制表符等
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 类型

【相关推荐:Go视频教程编程教学

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn