Go language learning notes 002 - basic data types

Go language basic data type

Go language is rich in data types. In addition to the basic integer, floating point, Boolean and string types, it also has arrays, slices, structures, functions, map s, channel s, etc. The basic types of go language are similar to other languages.

Basic data type


Integer types are divided into the following two categories:

Divided by length:


Corresponding unsigned integer:


uint8 is similar to byte type. int16 corresponds to short type in C language and int64 corresponds to long type in C language

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

Special integer

uintuint32 on a 32-bit operating system and uint64 on a 64 bit operating system
intint32 on a 32-bit operating system and int64 on a 64 bit operating system
uintptrUnsigned integer used to hold a pointer

Note: when using int and uint types, you cannot assume that they are 32-bit or 64 bit integers, but consider the possible differences between int and uint on different platforms.

Note: the length returned by the built-in len() function to obtain the length of the object can vary according to the byte length of different platforms. In practical use, the number of elements in a slice or map can be represented by int. When it comes to the structure description of binary transmission and read-write files, in order to keep the file structure from being affected by the byte length of different compilation target platforms, do not use int and uint.

Octal & hexadecimal

Binary numbers cannot be defined directly in Go language. Examples of octal and hexadecimal numbers are as follows:

package main

import "fmt"

func main() {
    // decimal system
    var a int = 10
    fmt.Printf("%d \n", a)  // 10
    fmt.Printf("%b \n", a)  // 1010 placeholder% b indicates binary
    // octal number system
    var b int = 077
    fmt.Printf("%o \n", b)  // 77
    // Hex starts with 0x
    var c int = 0xff
    fmt.Printf("%x \n", c)  // ff
    fmt.Printf("%X \n", c)  // FF


The Go language supports two floating-point numbers: float32 and float64.

The two floating-point data formats follow the IEEE754 standard: the maximum range of floating-point numbers of float32 is about bit 3.4e38, which can be defined with a constant: math.MaxFloat32.float64, and the maximum range of floating-point numbers of math.MaxFloat64 is about 1.8e308, which can be defined with a constant: math.MaxFloat64.

When printing floating-point numbers, you can use fmt package with% f:

package main
import (

func main() {
    fmt.Printf("%f \n", math.Pi)
    fmt.Printf("%.2f\n", math.Pi)
    fmt.Printf("%f \n", math.MaxFloat32)


complex64 and complex128

var c1 complex64
c1 = 1 + 2i
var c2 complex128
c2 = 2 + 4i 

The complex number has real and imaginary parts. The real and imaginary parts of complex64 are 32 bits, and the real and imaginary parts of complex128 are 64 bits.

Boolean value

In Go language, bool type is used to declare Boolean data. Boolean data has only two values: true and false.

be careful:

  1. The default value of Boolean variables is false.
  2. Casting integers to Booleans is not allowed in the Go language.
  3. Booleans cannot participate in numeric operations and cannot be converted to other types.

character string

Strings in the Go language appear as native data types. Using strings is like using other native data types (int, bool, flaot32, float64, etc.). The internal implementation of strings in Go language uses utf-8 encoding. The value of the string is the content in double quotation marks (""), and non ASCII strings can be directly added to the source code of Go language.

s1 := "Hello Golang"
// Individual letters, Chinese characters and symbols represent a character
s2 := "Hello!"
// Byte: 1byte=8bit (8 binary)
// 1 character 'B' = 1 byte
// The Chinese character "you" of one UTF-8 character generally takes up 3 bytes

String escape character

Common escape characters of strings in Go language include carriage return, line feed, single and double quotation marks, tabs, etc., as shown in the following table.

Escape charactermeaning
\rCarriage return (return to the beginning of the line)
\nNewline character (jump directly to the same column position of the next row)
\'Single quotation mark
\"Double quotation mark

Multiline string

s3 := `
	I am golang. 
s4 := `\r\n\`  // String as is without escape

Common operations of string

len(str)Find length
+Or fmt.SprintfSplice string
strings.containsDetermine whether to include
strings.HasPrefix,strings.HasSuffixPrefix / suffix judgment
strings.Index(),strings.LastIndex()Where the substring appears
strings.Join(a[]string, sep string)join operation

byte and run types

The elements that make up each string are called "characters", which can be obtained by traversing or obtaining a single string element. The characters are wrapped in single quotation marks' '.

var a := 'ah'
var b := 'b'

There are two types of Go language characters:

  1. uint8 type, or byte type, represents a character of ASCII code.
  2. rune type, representing a UTF-8 character

When Chinese or other characters need to be displayed, the rune type needs to be used. The rune type is actually an int32.

Go uses a special run type to handle Unicode, which makes Unicode based text processing more convenient. It can also use byte type for default string processing, taking care of performance and expansibility.

package main

import "fmt"

func traversalString() {
    s := "hello China"
    for i := 0; i < len(s); i++ {
        fmt.Printf("%v(%c) ", s[i], s[i])
    for _, r := range s {
        fmt.Printf("%v(%c) ", r, r)

func main() {

The bottom layer of the string is a byte array, so it can be converted with [] byte type. The string cannot be modified. The string is composed of byte bytes, so the length of the string is the length of byte bytes. The run type is used to represent utf8 characters. A run character is composed of one or more bytes.

Modify string

To modify a string, first convert it to [] run or [] byte, and then convert it to string. Either conversion will reallocate memory and copy the byte array.

func changeString() {
    s1 := "asd"
    byteS1 := []byte(s1)
    byteS1[0] = 'p'
    s2 := "cucumber"
    runeS2 := []rune(s2)
    runeS2[0] = 'west'

Type conversion

In Go language, there is only forced type conversion and no implicit type conversion. This syntax can only be used when two types support mutual conversion.

The basic syntax of cast is as follows


Where T represents the type to be converted. Expressions include variables, complex operators, function return values, etc.

For example, when calculating the hypotenuse length of a right triangle, the math package and the Sqrt() function are used. The function receives parameters of float64 type, and the variables A and b are of int type. At this time, it is necessary to convert the forced types of a and b to float64 type.

package main

import (

func sqrtDemo() {
    var a, b := 3, 4
    var c int
    // The parameter received by math.Sqrt() is of float64 type and needs to be cast
    c = int(math.Sqrt(float64(a*a + b*b)))
func main() {

Gets the number of Chinese characters in the string

Write code to get the number of Chinese characters in the string

package main

import "fmt"

func chineseNum(s1 string) int {
    num := 0
    for _, item := range s1 {
        if item > 127 {
    return num

func main() {
    n := chineseNum("a Ah, baking soda is really big")

Tags: C Go

Posted on Fri, 10 Sep 2021 19:11:11 -0400 by the_manic_mouse