go language basic data types

Keywords: C Go

go language basic data types

Go is a strongly typed language. This means that each variable you declare is bound to a specific data type and accepts only values that match this type.

Go has four types of data:

  • Basic types: numbers, strings, and Booleans
  • Aggregate types: arrays and structures
  • Reference types: pointer, slice, map, function, and channel
  • Interface type: Interface

In this module, we only cover basic types. If you don't know what other types are, don't worry. We will introduce it in the following modules.

First, let's explore the data types of values.

Integer number

In general, the key that defines the integer type is int. However, Go also provides int8, int16, int32, and int64 types with sizes of 8, 16, 32, or 64 bit integers, respectively. When using a 32-bit operating system, if only int is used, the size is usually 32 bits. On 64 bit systems, the int size is usually 64 bits. However, this behavior may vary from computer to computer. You can use uint. However, use this type only if you need to represent a value as an unsigned number for some reason. In addition, Go provides uint8, uint16, uint32, and uint64 types.

typeValue rangeMemory usage
int 8-128 to 1278 bits (1 byte)
uint 80 to 25518 bits (1 byte)
int 16-32 768 to 32 76716 bits (2 bytes)
uint 160 to 65 53516 bits (2 bytes)
int 32-2 147 483 648 to 2 147 483 64732 bits (4 bytes)
uint 320 to 4 294 967 29532 bits (4 bytes)
int 64-9 223 372 036 854 775 808 to 9 223 372 036 854 775 80764 bit (8 bytes)
uint 640 to 18 446 744 073 709 551 61564 bit (8 bytes)

In most cases, you will use int, but you need to understand other integer types, because in Go, int is different from int32, even if the natural size of an integer is 32 bits. In other words, when you need a cast, you need to do an explicit cast. If you try to perform a mathematical operation between different types, an error will occur. For example, suppose you have the following code:

var integer16 int16 = 127
var integer32 int32 = 32767
fmt.println(integer16 + integer32)

When you run the program, you will receive the following error:

output

invalid operation: integer16 + integer32 (mismatched types int16 and int32)
package main

import "fmt"

func main() {
    // Unsigned integer. The default value is 0
    var u8 uint8
    var u16 uint16
    var u32 uint32
    var u64 uint64
    fmt.Printf("u8: %d, u16: %d, u32: %d, u64: %d\n", u8, u16, u32, u64) // The default value is 0
    u8 = 255
    u16 = 65535
    u32 = 4294967295
    u64 = 18446744073709551615
    fmt.Printf("u8: %d, u16: %d, u32: %d, u64: %d\n", u8, u16, u32, u64)

    // integer
    var i8 int8
    var i16 int16
    var i32 int32
    var i64 int64
    fmt.Printf("i8: %d, i16: %d, i32: %d, i64: %d\n", i8, i16, i32, i64) // The default value is 0
    i8 = 127
    i16 = 32767
    i32 = 2147483647
    i64 = 9223372036854775807
    fmt.Printf("i8: %d, i16: %d, i32: %d, i64: %d\n", i8, i16, i32, i64)

    // Int type. The value range is int32 for 32-bit systems and int64 for 64 bit systems. The values are the same but different types
    var i int
    //i = i32 / / an error is reported. The compilation fails. The types are different
    //i = i64 / / an error is reported. The compilation fails. The types are different
    i = -9223372036854775808
    fmt.Println("i: ", i)

    // Floating point type, f32 precision 6 decimal places, f64 precision 15 decimal places
    var f32 float32
    var f64 float64
    fmt.Printf("f32: %f, f64: %f\n", f32, f64) // The default value is 0.000000
    f32 = 1.12345678
    f64 = 1.12345678901234567
    fmt.Printf("f32: %v, f64: %v\n", f32, f64) // Last bit rounding, output: f32: 1.1234568, f64: 1.1234567890123457

    // Plural form
    var c64 complex64
    var c128 complex128
    fmt.Printf("c64: %v, c128: %v\n", c64, c128) // The default values of real and imaginary numbers are 0
    c64 = 1.12345678 + 1.12345678i
    c128 = 2.1234567890123456 + 2.1234567890123456i
    fmt.Printf("c64: %v, c128: %v\n", c64, c128) // Output: c64: (1.1234568+1.1234568i), c128: (2.1234567890123457+2.1234567890123457i)

    // character
    var b byte                                       // uint8 alias
    var r1, r2 rune                                  // uint16 alias
    fmt.Printf("b: %v, r1: %v, r2: %v\n", b, r1, r2) // The default value is 0
    b = 'a'
    r1 = 'b'
    r2 = 'word'
    fmt.Printf("b: %v, r1: %v, r2: %v\n", b, r1, r2) // Output: B: 97 (number in ASCII), R1: 98 (number in UTF-8), R2: 23383 (number in UTF-8)

    b = u8
    r1 = i32
    fmt.Printf("b: %v, r1: %v\n", b, r1) // Output: b: 255, r1: 2147483647

    // Pointer address
    var p uintptr
    fmt.Printf("p: %v\n", p) // The default value is 0
    p = 18446744073709551615 // 64 bit system maximum
    //p = 18446744073709551616 / / an error is reported: the maximum value is exceeded
    fmt.Printf("p: %v\n", p)

}

Floating point number

The Go language provides two precision floating-point numbers, float32 and float64.
The values of these floating-point types can range from very small to very large. The limit value of float value range can be found in math package:
The constant math.MaxFloat32 represents the maximum value that float32 can get, which is about 3.4e38;
The constant math.MaxFloat64 represents the maximum value that float64 can get, which is about 1.8e308;
The minimum values that float32 and float64 can represent are 1.4e-45 and 4.9e-324, respectively.
A floating-point number of float32 type can provide the precision of about 6 decimal numbers, while float64 can provide the precision of about 15 decimal numbers,
Generally, float64 should be used first, because the cumulative calculation error of float32 is easy to spread, and the positive integer that float32 can accurately represent is not very large.

When printing floating-point numbers with the Printf function, you can use "% f" to control how many decimal places to keep

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

Boolean

Boolean types can have only two values: true and false. You can declare Boolean types using the keyword bool.

String (string)

In Go, the keyword string is used to represent the string data type. To initialize string variables, you need to define values in double quotation marks ("). Single quotation marks (') are used for single characters. The default encoding of Go language is UTF-8.

package main

import "fmt"

func main() {
    var str1 string // The default value is empty string ''
    str1 = `hello world`
    str2 := "Hello world"

    str := str1 + " " + str2 // String connection
    fmt.Println(str1)
    fmt.Println(str2)
    fmt.Println(str) // Output: hello world Hello World

    // Traversal string
    l := len(str)
    for i := 0; i < l; i++ {
        chr := str[i]
        fmt.Println(i, chr) // The coded number corresponding to the output character
    }
}

Sometimes you need to escape a character. To do this, in Go, use a backslash (\) before the character. For example, the following is the most common example of using escape characters:

  • \n: Xinxing
  • \r: Carriage return
  • \t: One tab stop
  • \': single quotation mark
  • \": double quotation marks
  • \: backslash

Default value

So far, almost every time a variable is declared, it is initialized with a value. But unlike in other programming languages, in Go, if you don't initialize variables, all data types have default values. This function is very convenient because you don't need to check whether the variable is initialized before using it.

The following is a list of the default values of the types we have browsed through so far:

  • 0 of type int (and all its subtypes, such as int64)
  • + 0.000000e+000 of float32 and float64 types
  • false of bool type
  • Null value of type string

Posted by phplearner on Sun, 03 Oct 2021 18:43:26 -0700