Home  >  Article  >  Backend Development  >  Reference data types and value data types in Go language

Reference data types and value data types in Go language

PHPz
PHPzOriginal
2023-06-01 09:12:11748browse

Go language is a strongly typed language, in which data types can be divided into two types: reference data types and value data types. Reference data types and value data types are slightly different in use. Let's take a closer look at these two data types.

1. Reference data types

Reference data types in Go language include slices, maps, channels, interfaces and pointers. For reference data types, the value of a variable is not just its own value, but a pointer to a memory address. Therefore, when we declare a variable of reference type, a memory address is allocated to it and stored on the stack, and the space pointed to by the memory address is stored in the heap.

  1. Slice

A slice is a dynamic array that can automatically grow or shrink as needed. Taking a string slice as an example, the declaration and initialization are as follows:

var s1 []string //Declare the slice
s1 = make([]string, 3) //Use the make function to initialize the slice
s2 := []string{"foo", "bar", "baz"} // Directly declare the slice and initialize it

In the Go language, a slice is a pointer to the underlying data, and also contains The length and capacity of the slice, which are the values ​​returned by the len and cap functions. When the number of elements in a slice exceeds the capacity, the Go language will reallocate a larger memory space and then copy the original elements to the new memory space.

  1. Mapping

Mapping is an unordered collection of key-value pairs, defined using the map keyword in the Go language. Taking a string mapping as an example, the declaration and initialization are as follows:

m1 := make(map[string]int) // Use the make function to initialize the mapping
m2 := map[string]int{" foo": 1, "bar": 2, "baz": 3} // Directly declare the mapping and initialize it

In the Go language, the mapping is also a pointer to the underlying data, and also contains the mapping elements quantity. When the number of elements in the map exceeds the underlying memory capacity, the Go language will reallocate a larger memory space.

  1. Channel

Channel is a way to transfer data between multiple Ctrips. Use the make function and <-operator to declare a channel in Go language . Taking a string channel as an example, the declaration and initialization are as follows:

var ch1 chan string // Declare the channel
ch1 = make(chan string) // Use the make function to initialize the channel
ch2 := make(chan string, 3) // Use the make function to initialize the buffered channel
ch1 <- "foo" // Send data to the channel
data := <-ch1 // Read from the channel Fetching data

In the Go language, the channel is also a pointer to the underlying data, which also contains the capacity of the channel and the current number of elements. When the number of elements in the channel exceeds the capacity, the Go language will block the current Ctrip and wait for other Ctrips to take away the elements.

  1. Interface

An interface is a data type that defines a set of interface methods. Taking a simple interface as an example, the declaration is as follows:

type Caller interface {
Call() bool
}

In the Go language, the interface is also a pointer to the underlying data. , pointing to the structure when implementing the interface. Interfaces are faster than value types because interface methods are not copied by implementation operations.

  1. Pointer

A pointer is a variable that contains a memory address. The & and * operators are used to declare and use pointers in the Go language. Take a pointer to a string type variable as an example, declare and use it as follows:

var p1 *string // Declare the pointer
s1 := "foo"
p1 = &s1 // Change the pointer Points to the memory address of the string variable
fmt.Println("s1:", s1, "p1:", *p1) // Get the value of the string variable through the pointer

In the Go language, The variable pointed to by the pointer needs to be declared in advance. There is no pointer operator in Go language similar to that in C language.

2. Value data types

Value data types in Go language include boolean, integer, floating point, complex number, character, string and array. For variables of value type, the value of the variable and the variable itself are stored on the stack.

  1. Boolean type

The Boolean type has only two values ​​​​in the Go language: true and false. Since the Boolean type has only one byte, it can effectively reduce the memory footprint when used in large quantities.

  1. Integer type

The integer type of Go language is divided into signed integer type and unsigned integer type. Signed integer type is divided into int8, int16, int32 and int64. , unsigned integer types are divided into uint8, uint16, uint32 and uint64. When using, it is recommended to use int and uint types as much as possible, because the byte width of these two types is the same on different platforms.

  1. Floating point type

The floating point type of Go language is divided into two types: float32 and float64. The default is float64 type. When using it, it is recommended to use the float64 type whenever possible, because the calculation speed and accuracy of this type are higher than the float32 type.

  1. Complex number type

Complex number type uses complex64 and complex128 types in Go language to represent real numbers and imaginary numbers. For example, the complex64 type indicates that both the real part and the imaginary part are complex numbers of type float32.

  1. Character type

Character type is represented by rune in Go language. The range of rune type and int32 type identification are the same. In Go language, you can use single quotes to represent characters.

  1. String

String is a read-only sequence of characters in Go language. Double quotes are used to represent strings in Go language. Strings in Go language are UTF-8 encoded.

  1. Array

Array is a data type with fixed length and the same element type in Go language. When using it, you need to declare the length and element type of the array in advance.

Conclusion

The reference data type and the value data type are slightly different in use in the Go language. Through comparison, it can be found that the value data type is stored in the stack, and the location of the variable in the memory does not vary. changes over time (if pointer operations are not used), and the value of a reference data type variable is a pointer pointing to a memory address allocated in the heap. In actual use, reference data types and value data types should be used rationally to achieve optimal performance and effects.

The above is the detailed content of Reference data types and value data types 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