Some keywords in Golang (defer, :=, go func())

Author : Feifei is a princess
Column : "Golang"
blog homepage : https://blog.csdn.net/myf_666
Personality sign: Don't be lazy in good times, don't be discouraged in adversity, control the situation with your heart, everything can be accomplished. —— Zeng Guofan
insert image description here

1. defer

Defer means to delay the call, and the call is made before the program returns.

If there are multiple defers in the program, the call sequence is similar to the stack, the one defined later is executed first, and the one defined first is executed later.


2. The difference between var and :=

1. was

  1. Declare variables as follows:
// 定义一个名称为“variableName”,类型为"type"的变量
// var variableName type
var number int
  1. define and initialize variables
// 初始化“variableName”的变量为“value”值,类型是“type”
// var variableName type = value
var number int = 10
  1. Defining and initializing multiple variables at the same time - parallel assignment
/* 定义三个类型都是"type"的变量,并且分别初始化为相应的值
    vname1为v1,vname2为v2,vname3为v3
    */
// var vname1, vname2, vname3 type = v1, v2, v3
var number1, number2, number3 int = 1, 2, 3

2. :=

Compared with var, the advantage of := is that the variable type can be automatically deduced, as follows:

/* 
定义三个变量,它们分别初始化为相应的值    
vname1为v1,vname2为v2,vname3为v3    
编译器会根据初始化的值自动推导出相应的类型
*/
// vname1, vname2, vname3 := v1, v2, v3
number1, number2, number3 := 1, 2, 3

3. The difference between the two

:=This notation directly replaces varand type, and this form is called a short statement. However, it is worth noting that it can only be used inside a function; if used outside a function, it will fail to compile, so var is generally used to define global variables.

In other words, ":=" can only be used when declaring "local variables", while "var" does not have this restriction. 1


Three, go func

The go keyword is equivalent to opening a new thread, which is called a goroutine in the go language. Since a new thread is opened, it runs in parallel with the main thread. For details, see the following sample program:

package main

import (
	"fmt"
	"time"
)

func main() {
    
    
	go spinner(100 * time.Millisecond)
	const n = 45
	fibN := fib(n) // slow
	fmt.Printf("\rFibonacci(%d) = %d\n", n, fibN)
}

func spinner(delay time.Duration) {
    
    
	for {
    
    
		for _, r := range `-\|/` {
    
    
			fmt.Printf("\r%c", r)
			time.Sleep(delay)
		}
	}
}

func fib(x int) int {
    
    
	if x < 2 {
    
    
		return x
	}
	return fib(x-1) + fib(x-2)
}

Here's a program that calculates Fibonacci, calculating the value of the 45th term. Since the recursive algorithm is used for implementation, it takes a certain amount of time. We use the spinner function to create a new thread (or goroutine) to dynamically display the running status of the program.

The result of the program running is as follows:
insert image description here

The final output is as follows:

insert image description here

It is worth noting that the function fmt.Printf("\r%c", r)in "\r%c"the statement is: the string formatting statement, which continuously outputs characters at the beginning of the line. Since it is at the beginning of the line, it is continuously overwritten and will not be output backwards, resulting in the dynamic effect of rotating the small wheel in place. .

Among them, "\n"it is a newline character and "\r"a carriage return character (return to the beginning of the line). Since we output in turn "-\|/", it produces a dynamic effect. For "\r"a detailed introduction, please refer to: https://blog.csdn.net/myf_666/article/details/128731608


  1. https://www.php.cn/be/go/439542.html ↩︎

Guess you like

Origin blog.csdn.net/myf_666/article/details/128730478