Basic Data Types in the Go Language

There are several basic data types commonly used in the Go language:

  • integer
  • float
  • Boolean type
  • complex
  • Character string


  • Signed
    • int8
    • int16
    • int32
    • int64
  • Unsigned
    • uint8
    • uint16
    • uint32
    • uint64
  • Special Integer
    • Uint, 32-bit operating system is uint32, 64-bit operating system is uint64
    • Int, 32-bit operating system is int32, 64-bit operating system is int64
    • uintptr, unsigned integer, for holding pointers

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


// Program Entry
func main() {
    // age = "17"
    // fmt.Println("Hello World")
    // fmt.Print("isOK")
    // fmt.Printf("Age: %s", age)\n
    var a1 = 10
    fmt.Printf("Decimal system:%d\n", a1)
    fmt.Printf("Binary:%b\n", a1)
    fmt.Printf("Octal number system:%o\n", a1)
    fmt.Printf("Hexadecimal:%x\n", a1)

    // Define octal directly
    a2 := 077
    fmt.Printf("Declare octets directly:%d\n", a2)
    // Define hexadecimal directly
    a3 := 0x12321
    fmt.Printf("Declare hexadecimal directly:%d\n", a3)


The Go language supports two floating-point types:

  • float32, defined by the constant math.MaxFloat32;
  • float64, defined by the constant math.MaxFloat64;

Print floating point numbers using the fmt package with%f as follows:

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

Boolean type

Boolean data is declared as bool type in the Go language. Boolean data has only true (true) and false (false) values.

Be careful:

  1. The default value for Boolean type variables is false.
  2. Coercion of integers to Booleans is not allowed in the Go language.
  3. Boolean types cannot participate in numerical operations or convert to other types.


Complex numbers have real and imaginary parts, complex64 has 32 bits of real and imaginary parts, and complex128 has 64 bits of real and imaginary parts.The following:

func main(){
    var c1 complex64
    c1 = 1 + 2i
    var c2 complex128
    c2 = 2 + 3i


Strings in the Go language appear as native data types, just like other native data types (int, bool, float32, float64, and so on).The internal implementation of strings in the Go language uses UTF-8 encoding.The value of the string is the content in double quotation marks ("). Non-ASCII characters can be added directly to the source code of the Go language, for example:

s1 := "Joker"
s2 := "HelloWorld"

If it is a multi-line string, it is inverted, and its contents are output as is, for example:

s1 := `


The elements that make up each string are called characters, enclosed in'', as follows:

s1 := 'H'
s2 := 'you'

There are two types of characters in the Go language:

  • uint8, or byte, represents a symbol of ASCII code;
  • rune type, representing a UTF-8 symbol;

The rune type is required when Chinese, Japanese, or other compound characters need to be processed.The rune type is actually an int32.

Go uses a special rune type to handle Unicode, making Unicode-based text easier to process, or byte for default string processing, with performance and scalability in mind.

package main

import "fmt"

func main() {
    s1 := "Hello World,How do you do!"
    // fmt.Printf(s1)
    // Traversal string, Byte type
    for i := 0; i < len(s1); i++ {
        fmt.Printf("%v(%c)", s1[i], s1[i])
    // Traversal string, rune type
    for _, r := range s1 {
        fmt.Printf("%v(%c)", r, r)

The output is as follows:

72(H)101(e)108(l)108(l)111(o)32( )87(W)111(o)114(r)108(l)100(d)239(ï)188(¼)140()228(ä)189(½)160( )229(å)165(¥)189(½)229(å)149(
72(H)101(e)108(l)108(l)111(o)32( )87(W)111(o)114(r)108(l)100(d)65292(,)20320(you)22909(good)21834(ah)65281(!)

As you can see from the above, the first method of traversal is byte-by-byte. For non-English letters or numbers, it takes up not one byte. For example, Chinese takes up three bytes in UTF-8 encoding. If we traverse by byte, we will get the first result, which is not what we want.We need a second traversal, where the rune type is used to represent UTF-8 characters, and a rune character consists of one or more bytes.

String Escape

Special strings need to retain their native meaning, so they need to be escaped as follows:

escape character Meaning
\r Enter
\n Line Break
\t Tabulation
\' Single quotation mark
\\ Backslash
\" Double Quotes

For example:

package main

import "fmt"

func main() {
    s1 := "\"Hello World\""

Common operations on strings

Method Effect
len(s) Find Length (Byte Length)
+or fmt.Sprintf StringBuilder
strings.Split String Cutting
strings.contains Determines whether a string contains
strings.HasPrefix,strings.HasSuffix Determine prefix/suffix of string (Boolean type)
strings.Index(),strings.LastIndex() Location where output substring occurs (index)
strings.Join(a[]string, sep string) join operation


package main

import "fmt"
import "strings"

func main() {
    s := "Hello World How do you do!"
    // Find Length
    // StringBuilder
    s1 := "Joker"
    s2 := "Hello"
    fmt.Println(s1 + s2)
    s12 := fmt.Sprintf("%s%s", s1, s2)
    // String Cutting
    sSplit := strings.Split(s, " ")
    // Determine whether a string contains
    fmt.Println(strings.Contains(s, "H"))
    fmt.Println(strings.Contains(s, "L"))
    // Determine the prefix of a string
    fmt.Println(strings.HasPrefix(s, "H"))
    // Determine the suffix of a string
    fmt.Println(strings.HasSuffix(s, "ah"))
    // Determine where the string appears
    fmt.Println(strings.Index(s, "H"))
    // Determine the last occurrence of a substring
    fmt.Println(strings.LastIndex(s, "o"))
    // join operation
    fmt.Println(strings.Join(sSplit, "-"))

String Modification

To modify a string, you need to first convert it to []rune or []byte, and then to string.In either case, memory is reallocated and the byte array is copied.

package main

import "fmt"

func main() {
    s1 := "hello"
    // 1. Coercion
    byteS1 := []byte(s1)
    // 2. Modify
    byteS1[0] = 'H'
    // 3. Force conversion to string and print

    s2 := "I love you China"
    // 1. Coercion
    runeS2 := []rune(s2)
    // 2. Modification
    runeS2[3] = 'Ancestor'
    // 3. Force conversion to string and print

Type Conversion

In the Go language, there are only casts, not implicit casts.This grammar can only be used when converting between two types is supported.

The basic syntax for cast is as follows:

T (expression)

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

Tags: Go encoding ascii

Posted on Sun, 15 Mar 2020 09:15:42 -0700 by purtip3154