This is the fourth day of my participation in Gwen Challenge

The previous article introduced Go’s basic syntax, declaration and initialization of variables. This article will cover the Go native data types in detail. Golang has a wealth of data types. The basic types are integer, floating point, Boolean, string, etc. In addition, there are slices, structs, Pointers, channels, maps, arrays and other types.

Native data type

In this article we focus on the basic types of Golang. There are two main types of integers:

  • According to the length of the integer: int8, int16, int32, int64
  • Uint8, uint16, uint32, uint64

In addition, Golang also provides platform-matched int and uint types. Integer types can be converted to each other. Length interception occurs when an integer type is converted from a high-length type to a low-length type, and only the low-length value of the high-length type is retained, resulting in conversion errors. Therefore, pay attention to actual use. See the following example:

var e uint16 = math.MaxUint8 + 1
fmt.Printf("e valud(unit16) is %v\n", e)
var d  = uint8(e)
fmt.Printf("d valud(unit8) is %v\n", d)
Copy the code

The output is as follows:

e valud(unit16) is 256
d valud(unit8) is 0
Copy the code

Because the storage mode of 256 at the bottom of uint16 is 00000001 00000000, after converting to Uint8, only the last 8 bits are intercepted, resulting in D changing to 00000000, that is, 0.

Floating point types come in two main types:

  • Float32, the maximum range for 3.40282346638528859811704183484516925440 e
  • 1.797693134862315708145274237317043567981 e+308 float64, maximum range

Print floating-point type precision is consistent with C language, can be used with %f, as shown in the following example:

fmt.Printf("%f\n", math.E)		// Output to the default width and precision
fmt.Printf("%.2f\n", math.E)	// Use the default width and 2-bit precision for output
Copy the code

Conversion can also be performed between float32 and float64, but you still need to pay attention to the loss of accuracy during conversion.

Golang’s Boolean forms are commonly known as True and Fasle. Unlike C, Golang’s Booleans cannot be strongly transformed from integers or involved in numerical calculations.

In Golang, strings appear as native data types, equivalent to other primitive types (integers, Booleans, etc.), and are based on UTF-8 encoding, so we need to distinguish between byte and rune when iterating over strings.

f := "Golang programming"
fmt.Printf("byte len of f is %v\n".len(f))
fmt.Printf("rune len of f is %v\n", utf8.RuneCountInString(f))
Copy the code

The output of the above example is:

byte len of f is 12
rune len of f is 8
Copy the code

The first method counts the length of byte, which is of type uint8 and represents an ASCII character. The second method counts the rune type, which is int32 and represents a UTF-8 character, which is analogous to the Java char type. Since Chinese characters occupy three bytes in UTF-8, len yields six bytes of Chinese characters, while utf8.runecountinString () counts the number of Unicode characters in the string.

To traverse each byte of a string, we can do the following:

f := "Golang programming"
// Iterate over the string in bytes
for _, g := range []byte(f){
	fmt.Printf("%c", g)
}
Copy the code

The output is:

Golangc fell – ¨ cCopy the code

During byte traversal, The Unicode characters of Chinese characters will be truncated, resulting in garbled characters. To ensure that each character is printed correctly, each character in the string can be iterated through rune, as shown below:

// Iterate over the string by character
for _, h := range f{
	fmt.Printf("%c", h)
}
Copy the code

The output is the desired Golang programming string.

summary

This article mainly introduces several commonly used data types of Go language, for learning a language, data types are very based on and must know the content. Especially the common integer, floating point, Boolean, string and so on. In this article, we will introduce the use of Pointers in the GO language.