Golang data type

After this section, we will formally learn the basics of the Golang language. In this section, we’ll cover “Golang datatypes,” and if you haven’t read the previous article, check out the link below

  1. Set up golang development environment
  2. Use of packages in Golang development

I. Classification of data types

The emergence of data types is to divide data into data with different memory sizes. When programming needs big data, it is necessary to apply for large memory, so that memory can be fully utilized.


1. Data categories

Golang data types There are the following data types by data category:

  • Boolean: Boolean values can only be constants true or false. A simple example: var b bool = true
  • Number types: int and floating-point float32 and float64. Go language supports integer and floating-point numbers, and supports complex numbers. The bit operation uses complement code.
  • String types: A string is a concatenated sequence of fixed-length characters. The Go string is concatenated by a single byte. The bytes of strings in the Go language use utF-8 encoding to identify Unicode text.
  • Derived types include: (a) Pointer (b) array (c) Struct (d) Channel (e) Function (f) slice (G) Interface (H) Map type.

2. Storage mode

Golang data types There are two broad categories of data types by way of storage:

  1. Value types, also called basic data types: numeric types, bool, string, array, struct
  2. Reference data types: pointer, slice slice, pipe chan, map, and interface

Value types: Variables store values directly. Data of the value type is stored in the stack memory space, which is freed after the function is called. Reference type: A variable stores an address that stores the final value. Data that refers to data types is stored in heap memory space and reclaimed by GC.

2. Common data types

1. The bool type

Golang uses the bool keyword to declare Boolean data. Boolean values can only be true or false. Represents whether the condition holds (true) or the condition does not hold (false), a simple example follows:

var b bool = true
Copy the code
  • The default value for a Boolean variable is false

  • Casting integers to Booleans is not allowed in Golang

  • Booleans cannot participate in numeric operations and cannot be converted to other types

2. Number type

(1) Integer type

The value can be int, INT16, INT32, INT64, uINT, uint8, uint16, uint32, and uint64. The following table

  • Uint8: Unsigned 8-bit integer type (0 to 255)
  • Uint16: Unsigned 16-bit integer type (0 to 65535)
  • Uint32: Unsigned 32-bit integer (0 to 4294967295)
  • Uint64: Unsigned 64-bit integer (0 to 18446744073709551615)
  • Int8: Signed 8-bit integers (-128 to 127)
  • Int16: Signed 16-bit integers (-32768 to 32767)
  • Int32: Signed 32-bit integer (-2147483648 to 2147483647)
  • Int64: Signed 64-bit integer (-9223372036854775808 to 9223372036854775807)

The int/uint type of the Go language is not portable and its length depends on the machine word length of the host. Int, uint and uintptr are typically 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems.

(2) Floating point type

Floating-point number types are float32, float64, complex64, and complex128

  • Float32: IEEE-754 32-bit floating-point number
  • Float64: ieEE-754 64-bit floating-point number
  • Complex64:32-bit real and imaginary numbers
  • Complex128:64-bit real and imaginary numbers

(3) Other numeric types

Here are some more number types:

  • Byte: Similar to uint8, it represents a character of the ASCII code, which can also be described as an ASCII character type
  • Rune: Similar to INT32, represents a Unicode code point
  • Uintptr: Unsigned integer used to store a pointer

Utf-8 is an implementation of Unicode. One of the biggest features of UTF-8 is that it is a variable length encoding method. It can use 1 to 4 bytes to represent a symbol, varying the length of the byte depending on the symbol.

(4) Strong type transfer

Golang has only cast, not implicit cast. This syntax can only be used when conversions between two types are supported.

The basic syntax of cast type conversion is type(variable). The converted data includes variables, expressions, and return values of functions. The following code

var a int = 8
// Change the int type to int32
var b int32 = int32(a)
Copy the code

3. The value is a string

String definitions: var STR string Strings can be expressed in two ways: double quotes (“”); The backquotes, also known as literal symbols (‘ ‘), look like this

package main

import "fmt"

func main(a) {
	var str1 = "hello world"
	var str2 = 'The window of the moon is suspected to be frost on the ground looked at the moon down to think of home'

	fmt.Println("str1 = ", str1)
	fmt.Println("str2 = ", str2)
}
Copy the code

The result is as follows

pan@pandeMBP SRC % go run learn/test4 str1 = Hello world str2 = Look up at the moon and think of home pan@pandeMBP SRC %Copy the code

Third, summary

In this section, we introduce the classification of Golang data types and introduce several commonly used data types. In later chapters, we’ll learn more about and work with various data types.