The first article from the public number: Go programming time

“Go programming time”, a can take you to learn the Go language column, at the same time welcome to search my namesake public account [Go programming time] (beautiful layout is more suitable for reading), the first time to get Go language dry materials.

A series of reading

1. Set up the Go language development environment

2. Five ways to create variables in Go language

3. Integer and floating point in Go


1 byte and rune

Byte, which takes up 8 bits per byte, is essentially the same as uint8, which represents a character in the ACSII table.

The following code defines variables A and B of type byte and uint8 respectively

import "fmt"

func main(a) {
    var a byte = 65 
    Var c byte = '\101' where \ is a fixed prefix
    // Hexadecimal: var c byte = '\x41' where \x is a fixed prefix
    
	var b uint8 = 66
    fmt.Printf("A value: %c \nb value: %c", a, b)
    // Use the string function
    / / FMT. Println (" a value: ", string (a), "\ nb value:", string (b))
}
Copy the code

In the ASCII table, since the ASCII number for letter A is 65 and the ASCII number for letter B is 66, the above code could also be written like this

import "fmt"

func main(a) {
	var a byte = 'A'
	var b uint8 = 'B'
    fmt.Printf("A value: %c \nb value: %c", a, b)
}
Copy the code

Their output is the same.

The value of A: A; the value of B: bCopy the code

Rune takes up four bytes and has a total of 32 bits. Therefore, it is essentially the same as the uint32. It represents a Unicode character (Unicode is an encoding specification that can represent most characters worldwide).

import (
	"fmt"
	"unsafe"
)

func main(a) {
	var a byte = 'A'
	var b rune = 'B'
	fmt.Printf("A occupies %d bytes \ NB occupies %d bytes", unsafe.Sizeof(a), unsafe.Sizeof(b))
}
Copy the code

The output is as follows

A occupies 1 byte B occupies 4 bytesCopy the code

Since the byte type can represent a limited number of values, only 2^8=256. So if you want to express Chinese, you can only use the rune type.

Var name rune = 'c'Copy the code

As you may have noticed above, I used single quotes instead of double quotes when defining characters, whether byte or rune.

For those of you coming from Python, it’s important to note that single and double quotes are not equivalent in Go.

Single quotation marks are used to indicate characters. In the example above, if you use double quotation marks, it means that you are defining a string, and the assignment will be inconsistent with the previous declaration, which will cause errors at compile time.

cannot use "A" (type string) as type byte in assignment
Copy the code

Uint8 is the same as uint8, and rune is the same as uint32.

The reason for this is simple: uint8 and uint32 visually confuse the uint8 and uint32 as a number, but in reality they can also represent a character, so the byte and Rune alias types are created to eliminate this illusion.

2. The string

Strings are one of the most familiar data types. Defining the method is simple

var mystr string = "hello"
Copy the code

Byte and rune are both character types. If multiple characters are put together, they form a string.

For example, the ASCII code for hello is 104,101,108,108,111

import (
	"fmt"
)

func main(a) {
    var mystr01 sting = "hello"
	var mystr02 [5]byte = [5]byte{104.101.108.108.111}
    fmt.Printf("mystr01: %s\n", mystr01)
	fmt.Printf("mystr02: %s", mystr02)
}
Copy the code

Mystr01, like mystr02, illustrates the nature of a string, which is actually a byte array

mystr01: hello
mystr02: hello
Copy the code

From the above study, we know that characters are divided into byte and rune, which occupy different sizes.

How many bytes does China take up?

To answer this question, you need to know that the string of Go is encoded in UFT-8, so the English letter takes up one byte and the Chinese letter takes up three bytes, so the length of Hello, China, is 5+1+ (3 * 2)= 12 bytes.

import (
	"fmt"
)

func main(a) {
	var country string = "Hello, China's"
	fmt.Println(len(country))
}
/ / output
12
Copy the code

I’ve used double quotation marks to represent a string, but that’s not the only way to represent a string.

In addition to double quotation marks, you can also use backquotation marks.

In most cases, there is no difference between the two, but if you have an escape character \ in your string, notice here that they are different.

Strings wrapped in backquotes, the equivalent of Raw strings in Python, will ignore the escape inside.

For example, if I wanted to represent the string \r\n, I would write it like this, using double quotation marks. This is called explanatory notation

var mystr01 string = "\\r\\n"
Copy the code

It’s much easier to use back quotes, what you see is what you get, and this is called prototypical notation

var mystr02 string = `\r\n`
Copy the code

They all print the same

import (
	"fmt"
)

func main(a) {
	var mystr01 string = "\\r\\n"
	var mystr02 string = `\r\n`
	fmt.Println(mystr01)
	fmt.Println(mystr02)
}

// output
\r\n
\r\n
Copy the code

If you still want to use interpreted strings, it’s too much trouble to escape. You can use FMT %q to restore this.

import (
	"fmt"
)

func main(a) {
	var mystr01 string = `\r\n`
	fmt.Println(`\r\n`)
	fmt.Printf(The interpreted string for "is: %q", mystr01)
}
Copy the code

The output is as follows

The explanatory string \r\n is:"\\r\\n"
Copy the code

Backquotes can also represent a multi-line string without a newline character (because they can’t be written).

import (
	"fmt"
)

func main(a) {
	var mystr01 string = 'Hello! My public number is: Go programming time, welcome to pay attention to '

	fmt.Println(mystr01)
}
Copy the code

The output is as follows

How are you! My public number is: Go programming time, welcome your attentionCopy the code