Every language has its own data structure, and Go is no exception. There are four major categories: base types, aggregate types, reference types, and interface types. This article provides a brief overview of these types.

1. Basic types

The integer

The integral type of Go is divided into signed and unsigned integers.

Signed integers are divided into int8, int16, int32, int64

The unsigned integer is divided into uint8, uint16, uint32, and uint64

However, int and uint are usually used directly, because they are automatically converted to the most efficient type, depending on the characteristics of the platform. Nowadays, it is rare to see computer level below 32 bits, so int is usually 32 or 64 bits, depending on the platform and compiler.

Int is by far the most widely used numeric type. When you need to specify the number of digits explicitly, you can simply use int.

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

Uintptr and rune are two special types of numbers. The Uintptr is undefined in size and used to store Pointers. It is mainly used for low-level programming, such as the Go language and C language interaction.

If two values can be used == and! = to show that the two values are comparable.

Integers are comparable.

Other numeric types

In addition to integer data, there are two data types in Go: floating point and complex.

Floating-point numbers are of two types: FLOAT32 and float64. Floating-point numbers are usually represented by e or e in exponential notation.

The positive numbers of float32 range from 1.4E-45 to 3.4e38

The positive numbers of float64 range from 4.9E-324 to 1.8e308

In base 10, float64 has 15 significant digits, far more than the 5 digits of Float32. If there are no special circumstances, float64 should be preferred.

Complex numbers also have two types of complex64 and complex128, which consist of float32 and float64, respectively. The Math/CMPLX library provides the functions needed for complex number operations.

Floating-point numbers and complex numbers are comparable.

string

Strings are the most commonly used data type. Strings are composed of immutable sequences of bytes ([] bytes), and the contents of strings are typically processed in utF-8 encoding, as explained in detail in the previous article.

The string contents of Go are immutable, and operations on the current string generate a new string. Go strings naturally support UTF-8, and it is customary to use this encoding in preference to less garbled characters.

The Go language also provides a rich library of classes for strings:

  • Bytes: Used to manipulate slice ([]byte). Using byte.buffer is efficient if strings need to be modified frequently

  • Strings: Used for search, replace, and other character transfer operations

  • Strconv: Mainly used for converting between strings and other basic data types

  • Unicode: Properties used to determine characters, such as whether they are numbers, uppercase, and so on

Strings are comparable.

Boolean value

Booleans are relatively simple, with only two values: true and false. Usable! Symbol to perform the inverse operation,! True is false.

Can also use && and | | for combination operation, in the combination operation, following short circuit, short circuit behavior refers to the left if you can directly determine the final result, so the back of the calculation will not be as follows:

Var I = 1 / / I < 100 code will not be executed if I > 0 | | I < 100 {FMT. Printf (" result % + v ", I)}Copy the code

Boolean values are comparable.

2. Aggregation type

The value of an aggregate type consists of a set of variables in memory. Arrays and structures are aggregate types, and their lengths are fixed. The elements in an array must all be of the same type, while the elements in a structure can be different.

An array of

An array is a fixed-length sequence of zero or more of the same data type (no longer than the array length). Arrays must be declared with a specified length. You can use constants, but not variables.

In fact, we rarely use arrays directly in our code. Arrays are mostly used as the underlying storage for Slice and are not used directly.

The comparability of an array depends on the comparability of the elements, and if the elements are comparable, then the array is comparable, and vice versa.

The structure of the body

A structure is a data type composed of any variables of any type, similar to the concept of classes in other languages.

type Student struct {
    Name string
    age  int
}
Copy the code

Go language is an object-oriented language, but it abandons the concepts of classes and objects in Java. Structure is one of the foundations of object-oriented implementation in Go language, as well as interfaces, which will be discussed below.

In object-oriented Go, the concept of inheritance has been abandoned, but in structures, through structure nesting, it is also partially implemented.

The comparability of structures also depends on the comparability of variables within them.

3. Reference types

References are another data type, and many of the advanced features of the Go language rely on references. References refer indirectly to variables or states, and manipulating data by reference affects all references to that data.

Pointer to the

A pointer is a data type whose value is the address of a variable. You can have multiple Pointers to a variable, and use any of them to modify the data. The other Pointers also get the latest value.

i := 1
p1 := &i
p2 := &i

*p1 = 2

fmt.Println(*p2) // 2
Copy the code

Pointers are comparable.

slice

Slice is a variable-length sequence with the same elements. Slice looks like an array, but is fundamentally different.

Slice relies on arrays, and without arrays, there is no slice.

A slice has three attributes: pointer, length, and capacity. Where the pointer points to an element in the array (not necessarily the first one), which is the first element slice can access.

Length is the number of elements in slice and cannot exceed capacity, which is usually the length from the position of the slice pointer to the position of the last element in the underlying array.

Slice is not comparable and can only be compared to nil.

map

Map is a reference to a hash table.

The map is easy to use, but note that the key of the map must be comparable. If the key is not comparable, the value of the response cannot be queried using the key. The value type is unlimited and can be any value.

Map is not comparable and can only be compared to nil.

function

Function is the function that you’ll use when you write Go’s HelloWorld program. A function is also a reference type.

Function itself is not comparable and can only be compared to nil, but it can be compared using reflection to get a function pointer.

channel

The Go language naturally supports concurrency. A channel is a key part of this, and Goroutine is used to execute tasks concurrently. Channels are used to connect different Goroutines. A channel is also a reference type.

A channel is comparable.

4. Interface type

Another type in Go is the interface type.

Interfaces are key to Go’s object-oriented implementation. The interface type of Go is very special. You don’t need to implement an interface explicitly, you just implement the methods in the interface, and you implement the interface by default.

Interface types are comparable.

5. Summary

Go’s data type design is simple but extensible, allowing developers to dynamically extend data based on their needs, not just for aggregate data types like structures, but even for basic data types. In addition, Go comes with support for JSON, XML and Protocol Buffer, and there is no need to introduce external libraries, which makes writing programs very lightweight and can introduce as few dependent packages as possible.

The text/Rayjun

This article was first published on wechat official account [Rayjun]