This is the third Go column, and it’s still basic and simple. If you have programming experience, you probably know it at a glance. But if you are just beginning to contact programming, it is recommended to take a good look at the code demo in the article all run through. Only when the foundation is well laid can we stride forward to a higher goal.

Say no more, go ~

Go data types fall into four categories:

  1. Basic types: number, string, and Boolean.
  2. Aggregate types: array and struct.
  3. Reference types: pointer, slice, dictionary map, function func, and channel.
  4. Interface type: interface.

Among them, the basic types are divided into:

  1. Integer: int8, uint8, byte, INT16, uint16, INT32, uint32, INT64, uint64, int, uint, uintPtr.
  2. Floating-point: float32 and float64.
  3. Complex types: complex64, complex128.
  4. Boolean: bool.
  5. String: string.
  6. Character type: rune.

The integer

There are 12 types of integers, which are divided into signed and unsigned integers. For convenience, I have compiled a table here:

type Length (bytes) The scope of
int8 1 – 128 ~ 127
uint8 1 0 ~ 255
byte 1 0 ~ 255
int16 2 – 32768 ~ 32767
uint16 2 0 ~ 65535
int32 4 – 2147483648 ~ 2147483647
uint32 4 0 ~ 4294967295
int64 8 – 9223372036854775808 ~ 9223372036854775807
uint64 8 0 ~ 18446744073709551615
int 4/8 Same as above
uint 4/8 Same as above
uintptr 4/8 As above, uints sufficient to store Pointers

In general, we use int and uint when developing. Type INT8 is used only if there is an explicit need to specify a length.

Type conversion

For both arithmetic and logical operations, Go requires that the types of operands be consistent. If they are not, an error is reported.

var a int = 10
var b int32 = 20

Println(a + b) // Invalid operation: a + b (mismatched types int and int32)
Copy the code

The tools will remind you of this when you write the code, and will also write the cast code, which is great.

var a int = 10
var b int32 = 20
fmt.Println(a + int(b)) / / output 30
Copy the code

One thing to note is that there may be a loss of accuracy when a float is converted to an integer.

var c float32 = 10.23
fmt.Println(int(c)) / / output 10
Copy the code

The numerical computation

The arithmetic operators include +, -, *, /, and %.

The % modulo operator can only be used for integers where the sign of the modulo remainder is the same as the dividend.

/ / modulus
fmt.Println(5 % 3)   2 / / output
fmt.Println(- 5 % - 3) / / output - 2
Copy the code

The/division operator, if both operands are integers, then the result is an integer.

/ / division
fmt.Println(5 / 3)     / / output 1
fmt.Println(5.0 / 3.0) / / output 1.6666666666666667
Copy the code

Comparison operations

The comparison operators include: >, >=, <, <=, ==, and! =.

Different types are not comparable, but integers can be directly compared with literals.

// Compare
var i int32
var j int64
i, j = 1.2

If I == j {invalid operation: I == j (mismatched types int32 and int64)
// fmt.Println("i and j are equal.")
// }
if i == 1 || j == 2 {
	fmt.Println("equal.")}Copy the code

An operation

A operators include: &, |, ^ & ^, < < and > >.

This is not much to introduce, usually used in the development process is less.

Floating point Numbers

There are two floating-point types: FLOAT32 and FLOAT64.

The floating-point number surface is automatically inferred to be float64.

f := 10.0   // Automatically deduces float64
Copy the code

When comparing floating point numbers, you cannot directly use == and! =, the result will be unstable. You should use the Math standard library.

The plural

There are two complex types, complex64 and Complex128.

There are three built-in functions that operate on complex numbers. They are:

  1. complex: Construct a complex number.
  2. real: Gets the real part of the complex number.
  3. imag: Gets the imaginary part of the complex number.
/ / the plural
var x complex64 = 3 + 5i
var y complex128 = complex(3.5.10)
// Print the real and imaginary parts separately
fmt.Println(real(x), imag(x)) // Output 3 5
fmt.Println(real(y), imag(y)) // Output 3.5 10
Copy the code

Boolean value

The keyword for a Boolean type is bool, which has two values: true and false.

ok := true
fmt.Println(ok)
Copy the code

Boolean and integer types cannot be converted to each other.

var e bool
e = bool(1)	Cannot convert 1 (type untyped int) to type bool
Copy the code

The conditional part of if and for statements must be a value or expression of type Boolean. So if you’ve done a lot of Python writing, and you have to be aware of that, I’m going to flip over here.

m := 1
Non-bool m (type int) used as if condition
// fmt.Println("is true")
// }
if m == 1 {
	fmt.Println("m is 1")}Copy the code

string

The string keyword is string, which is also of a basic type.

Strings can be initialized directly with literals:

/ / string
s1 := "hello"
s2 := "world"
Copy the code

Use ‘to define unescaped raw strings, support newlines:

// Raw string
s := `row1\r\n
row2`
fmt.Println(s)
Copy the code

Concatenation string:

// String concatenation
s3 := s1 + s2
fmt.Println(s3)
Copy the code

Get string length:

// Take the length of a string
fmt.Println(len(s3))
Copy the code

To get a character by index subscript:

// Take a single character
fmt.Println(s3[4])
Copy the code

String slice:

 // Slice the string
fmt.Println(s3[2:4])
fmt.Println(s3[:4])
fmt.Println(s3[2:])
fmt.Println(s3[:])
Copy the code

Strings are not modifiable, so if you assign a value to a string, you will hear an error:

// Modify error
s3[0] = "H"	// cannot assign to s3[0] (strings are immutable)
Copy the code

Traversal string:

s4 := "Hello world"

// Iterate over the byte array
for i := 0; i < len(s4); i++ {
   fmt.Println(i, s4[i])
}

/ / output
/ / 0, 104
/ / 1, 101
/ / 2, 108
/ / 3, 108
/ / 4, 111
/ / 5 32
/ / 6, 228
/ / 7, 184
/ / 8, 150
/ / 9, 231
/ / 10, 149
/ / 11, 140

// Iterate over the rune array
for i, v := range s4 {
   fmt.Println(i, v)
}

/ / output
/ / 0, 104
/ / 1, 101
/ / 2, 108
/ / 3, 108
/ / 4, 111
/ / 5 32
/ / 6, 19990
/ / 9, 30028
Copy the code

The results show the difference between the two. The first one is a byte array. The second is a Unicode character traversal.

It is traversed as an array of bytes. The character type is byte and length is 1. Although the string is intuitively 8 in length, Chinese characters make up three characters in UTF-8 encoding, so the total length is 12.

It is traversed in Unicode mode and the character type is RUNe.

There are two character types supported in Go. One is the byte, uint8 alias that represents the single byte value of a UTF-8 string. The other is rune, an alias for INT32, which represents a single Unicode character.

Finally, the Go source files are encoded in UTF-8, so we must choose utF-8 as the encoding format, otherwise there may be some unexplained errors.

conclusion

This article mainly introduces five basic data types, which are: integer, floating point, complex, Boolean and string.

Among them, complex numbers are used less, integer and floating point numbers are mainly used in scenarios that require numeric types, and Boolean values are mostly used in conditional statements.

There is a lot of use of strings, and some standard libraries support them, such as bytes, Strings, Strconv, and Unicode.

If you have time, you should learn more about the standard library.


The brain map and source code in the article are uploaded to GitHub, students who need to download.

Address: github.com/yongxinz/go…

Follow the public account AlwaysBeta and reply to “Goebook” to pick up classic Go programming books.

List of Go columns:

  1. Development environment setup and VS Code development tools configuration
  2. Declaration and assignment of variables and constants