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

What data structures are there in go language?

青灯夜游
青灯夜游 Original
2022-12-16 14:00:23 5847browse

There are four major categories of go language data structures: 1. Basic types, including integers (signed and unsigned integers), floating point numbers, complex numbers, and strings (composed of immutable byte sequences), Boolean values (only two values: true and false); 2. Aggregation types, including arrays and structures (which are data types composed of any number of variables of any type); 3. Reference types, including pointers and slices (yes A variable-length sequence with the same elements), map, function, channel; 4. Interface type.

What data structures are there in go language?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Every language has its own data structure, and Go language is no exception. There are four major categories in total,Basic types, aggregate types, reference types and interface types. This article briefly introduces these types.

What data structures are there in go language?

1. Basic type

Integer type

Go’s integer type classification for signed and unsigned integers.

Signed integers will be divided into int8, int16, int32, int64

Unsigned integers will be divided into uint8, uint16, uint32, uint64

But usually int and uint are used directly , because these two types will automatically be converted into the most efficient type according to the characteristics of the platform. Computers below 32 bits are now rare, so int is usually 32 or 64 bits, but it depends on the platform and compilation The device decides. [Related recommendations:Go Video Tutorial,Programming Tutorial]

int is currently the most widely used numerical type. When you need to explicitly specify the number of digits in a value, directly Just use int.

Generally speaking, signed integers are sufficient, and unsigned integers are only used for bit operations or specific arithmetic.

uintptr and rune are two special numeric types. The size of uintptr is unclear and is used to store pointers. It is mainly used for low-level programming, such as the interaction between Go language and C language.

If two values can be compared using == and !=, it means that the two values are comparable.

Integers are comparable.

Other numeric types

In addition to integer data, there are two data types in the Go language:Floating point numbersandplural.

There are two types of floating point numbers: float32 and float64. Floating point numbers generally use exponential notation, represented by e or E.

The positive number range of float32 is 1.4e-45 ~ 3.4e38

The positive number range of float64 is 4.9e-324 ~ 1.8e308

In decimal system, The effective number of float64 is 15 digits, which is far more than the 5 digits of float32. If there are no special circumstances, float64 should be used first.

There are also two types of complex numbers, complex64 and complex128. These two types are composed of float32 and float64 respectively.math/cmplxThe library provides functions required for complex number operations.

Floating point numbers and complex numbers are comparable.

String

String is the most commonly used data type. A string is composed of an immutable byte sequence ([]byte). The content of the string is usually processed in UTF-8 encoding format, which has been explained in detail in the previous article.

Go's string content is immutable, and any operation on the current string will generate a new string. Go strings naturally support UTF-8, and it is customary to give priority to this encoding method, so there will be less trouble with garbled characters.

The Go language also provides a rich class library for strings:

  • bytes: used to operate byte slices ([]byte), if you need to manipulate strings For frequent modifications, using byte.Buffer will be efficient

  • strings: used for search, replacement and other character transfer operations

  • strconv: mainly Used for conversion between strings and other basic data types

  • unicode: used to determine the characteristics of characters, such as whether it is a number, whether it is uppercase, etc.

Strings are comparable.

Boolean value

Boolean value is relatively simple, with only two values:trueandfalse. You can use the ! symbol to perform negation operations, !true is false.

You can also use && and || to perform combined operations. In combined operations, short-circuit behavior will also be followed. Short-circuit behavior means that if the final result on the left side can be determined directly, then subsequent calculations will not be performed. , as follows:

var i = 1 // i < 100 代码就不会被执行 if i > 0 || i < 100 { fmt.Printf("result %+v", i) }

Boolean values are comparable.

2. Aggregation type

The value of the aggregate type consists of a set of variables in memory. Both arrays and structures are aggregate types, and the lengths of arrays and structures are fixed. The element typesin the array mustbethe same, while the elementsin the structure can be different.

数组

数组是一个长度固定,拥有0 个或多个(不超过数组长度)相同数据类型的序列。数组在声明的时候必须指定长度,可以使用常量,但是不能使用变量。

实际上,我们很少在代码中直接使用数组,数组在绝大部分的时候都是做为 slice 的底层存储,并不会直接使用。

数组的可比较性取决于元素的可比较性,如果元素是可比较的,那么数组也是可比较的,反之亦然。

结构体

结构体是由任意个任意类型的变量组合在一起的数据类型,和其他语言中的概念相似。

type Student struct { Name string age int }

Go 语言是一个面向对象的语言,但却又抛弃了 Java 中类和对象的概念,结构体是 Go 语言实现面向对象的基础之一,还有一部分是接口,下面会聊到。

在 Go 的面向对象中,已经摈弃了继承的概念,但在结构体中,通过结构体嵌套,也算是实现了部分继承的功能。

结构体的可比较性也取决于其中变量的可比较性。

3. 引用类型

引用是另外一种数据类型,很多 Go 语言的高级功能都依赖引用。引用都间接指向变量或者状态,通过引用来操作数据会让该数据的全部引用都受影响。

指针

指针是一种数据类型,指针的值是一个变量的地址。对于一个变量来说,可以有多个指针,通过其中任意一个指针来修改数据,通过其他指针也会获取到最新修改的值。

i := 1 p1 := &i p2 := &i *p1 = 2 fmt.Println(*p2) // 2

指针是可比较的。

slice

slice 是一个拥有相同元素的可变长度序列。 slice 看起来与数组很像,但本质上不同。

slice 依赖数组,没有数组,就没有 slice。

一个 slice 有三个属性,指针,长度和容量。其中指针指向数组中的某个元素(不一定是第一个),这是 slice 可以访问的第一个元素。

长度是 slice 中元素的个数,不能超过容量,容量通常是指 slice 指针的位置,到底层数组的最后一个元素的位置的长度。

slice 不可比较, 只能和 nil 比较。

map

map 是散列表的引用。

map 的使用很简单,但是需要注意一点,map 的 key 必须是可比较的,如果 key 不可比较,那就无法通过 key 查询到响应的 value,value 的类型是没有限制的,可以是任意值。

map 不可比较,只能和 nil 比较。

function

function 就是函数,在写 Go 的 helloworld 程序时,就会用到函数。 函数也是一种引用类型。

function 本身不可比,只能和 nil 比较,但是可以通过反射获取函数指针进行比较。

channel

Go 语言天然支持搞并发。而 channel 就是其中关键一环,goroutine 用来并发执行任务。而 channel 则用来连接不同的 goroutine。channel 也是属于引用类型。

channel 是可比较的。

4. 接口类型

Go 语言中还有一个类型是接口类型。

接口是 Go 实现面向对象的关键。Go 的接口类型很特别,你不需要去显示的实现一个接口,只要把接口中的方法实现,就默认实现了这个接口。

接口类型是可比较的。

5. 小结

Go 的数据类型设计简洁,但扩展性很好,开发者可以根据自己的需要动态的扩展数据,不只是对于结构体这种聚合数据类型,即使对于基础数据类型,也可以根据的需要进行扩展。另外 Go 自带对 JSON、xml 以及 Protocol Buffer 的支持,不需要引入外部的库,这就使得写程序时会很轻量级,可以尽可能少的引入依赖包。

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of What data structures 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