Go language learning to check for gaps and fill in gaps Day4

Go language learning to check for gaps and fill in gaps Day4

1. Master the use of iota

Consider the following code:

package main

import "fmt"

const (
	a = iota
	_
	b
	c = "ReganYue"
	d
	d1
	e = iota
	f = iota
)

func main() {
    
    
	fmt.Println(a, b, c, d, d1, e, f)
}

Think about what the output will be?

The output will not be published yet. Let's talk about iota first, iota is an identifier used to assign values ​​​​to constants that need to be self-increased. We can use underscores _to omit unwanted values. And we have assigned a value to the variable in the middle, and if the subsequent variable is not assigned, the value of the subsequent d and d1 will be the same as c. But if you mark a variable as iota again, it will be assigned a self-increasing value, and the value has been self-increasing since the first variable was defined here.

So the output is:

image-20211122134500118

Let's take a look at the usage of iota again:

package main

import "fmt"

const (
	A, B = iota, iota + 1
	C, D
	E, F
)

func main() {
    
    
	fmt.Println(A, B, C, D, E, F)
}

The result of the operation is:

image-20211122134806337

iota only grows on the next line, not every variable.

2. What types of variables can be assigned nil

var (
	A string = nil
	B int    = nil
	C interface{
    
    } = nil
	D chan int = nil
	E float64 = nil
	F func() = nil
	G []int = nil
	H map[int]int = nil
	I *int = nil
)

Do you know which types of variables above can be assigned nil values?

Obviously, A is not, the empty value of the string is "", not nil. B and E are not, they are numeric, and the value can only be a number.

Other types of variables, such as interface, chan, func, slice, map, and pointer, can be assigned nil as a null value.

3. Golang's init function

We all know that the main function is the entry function of the Golang program, but Golang also has a special function init function that you may not know, it is executed before the main function, and can implement operations such as initializing variables in the package.

The first thing you should know is that a package can have multiple init functions, and even a go file can have many init functions.

The second point: the init function in the same go file is called from top to bottom, and in the same package, the calling order of the init function is called according to the order of the file name, as for the calling order of the init function in different packages , if these packages do not depend on each other, they are called in the order of import in the main package. If they depend on each other, it depends on who is dependent first and who is initialized last.

The third point: the init function cannot be assigned to a function variable, nor can it be called, otherwise the compilation will fail.

Fourth point: If a package is referenced multiple times, the package will only execute the init() function once.

4. A note on assigning function return values

Let's look at a piece of code first:

package main

import "fmt"

func nh() []string {
    
    
	return nil
}
func main() {
    
    
	h := nh
	n := nh()
	if h == nil {
    
    
		fmt.Println("h is nil")
	} else {
    
    
		fmt.Println("h is not nil")
	}

	if n == nil {
    
    
		fmt.Println("n is nil")
	} else {
    
    
		fmt.Println("n is not nil")
	}
}

Let's take a look at the running results:

image-20211122143030943

Obviously, h := nhthe function nh is assigned to the variable h, and n := nh()it seems that the operation of assigning the function to n is to assign the return value of the function nh to n. So h is not nil and n is nil.

result:

[External link image transfer...(img-IfCWeUTI-1690453964829)]

Obviously, h := nhthe function nh is assigned to the variable h, and n := nh()it seems that the operation of assigning the function to n is to assign the return value of the function nh to n. So h is not nil and n is nil.

Guess you like

Origin blog.csdn.net/qq_36045898/article/details/131966318