Basic data types of go language learning

Time:2020-9-18

01 integer type

Integer types can be divided into signed integer and unsigned integer according to signed / unsigned. The two types are divided by size, including 8-bit, 16 bit, 32-bit and 64 bit

size 8 16 bits 32 bit 64 bit
Signed int8 int16 int32 int64
No sign uint8 uint16 uint32 uint64

In addition, there are two types of integers, int and uint, which are the same size as the native signed / unsigned integer types, both 32-bit or 64 bit.

Among them, int32 and uint8 have type aliases of run and byte, which can be used interchangeably. It is important to note that both int and uint are different from well-defined types, that is, int and int32 are two different types and must display transformations.

var a int
var b int32
var c rune
var d byte
var e uint8

func main() {
  fmt.Printf("a=%d type:%T\n", a, a)
  fmt.Printf("b=%d type:%T\n", b, b)
  fmt.Printf("c=%d type:%T\n", c, c)
  fmt.Printf("d=%d type:%T\n", d, d)
  fmt.Printf("e=%d type:%T\n", e, e)

  if c == b {
    fmt.Println ("Rune type is the type alias of int32, the two can be used interchangeably")
  }

  if d == e {
    fmt.Println ("byte type is the type alias of uint8, they can be used interchangeably")
  }
}

02 floating point type

There are two types of floating-point numbers, namely, float32 and float64. In most cases, it is recommended to use float64 first, because the operation of float32 is prone to errors.

var f float32
var g float64

func main() {
  fmt.Printf("f=%g type:%T\n", f, f)
  fmt.Printf("g=%g type:%T\n", g, g)
}

03 boolean type

There are only two possible values of bool type, true and false. In go language, the zero value of bool type is false, and Boolean value cannot be implicitly converted to a value of 0 or 1, and vice versa.

var h bool

func main() {
  fmt.Printf("h=%t type:%T\n", h, h)
}

04 string type

A string is an immutable sequence of bytes with double quotation marks. It can contain any data, including 0-value bytes. Immutability means that multiple strings can share a memory space.

Basic data types of go language learning
As shown in the figure, the same memory space contains the string s, the string Hello, and the string world.

String operation:

  • Len function

    The built-in len function returns the number of bytes of a string, not the number of characters. In simplified Chinese of UTF-8, one character takes up three bytes.

  • Subscript access character

    The i-th character can be obtained by accessing the string with a subscript.

  • Substring generation

    The substring generation operation s [I: J] generates a new string whose contents are taken from the bytes of the original string. The subscript starts from I (with boundary value) until J (without boundary value). The size of the new string is J-I bytes.

    It should be noted that if the subscript is out of bounds, or the value of J is less than I, the shutdown exception will be triggered.

    The default values of operands I and j are 0 [start position of string] and Len (s) [end position of string], respectively. If I or j are omitted, or both are omitted, the default values are taken.

  • String concatenation

    The plus (+) operator concatenates two strings to produce a new string.

var i string

func main() {
  fmt.Printf("i=%s type:%T\n", i, i)
  
  i = "hello golang"
  fmt.Printf ("length of string I% D / N", len (I))
  fmt.Printf "(" first character of the string I: C / N ", I [0])
  j := i[0:5]
  fmt.Printf ("value of substring J: S / N", J)
  k := i[5:]
  fmt.Printf ("value of substring K: S / N", K)
  l := i[:5]
  fmt.Printf ("value of substring L: S / N", l)
  m := i[:]
  fmt.Printf ("value of substring M: S / N", m)
  n := "hello "
  o := "world"
  p := n + o
  fmt.Printf ("value of connection string P% s / N", P)
}

Go Language Basics

Basic data types of go language learning