Go Language learning tour

Go is the C language of the 21st century

This paper is used to record the author’s experience in the process of learning Go language, which is convenient for future review and also provides some experience reference for others.

Three winter vacation period, this article begins with the author for a data structure, algorithm and computer network knowledge has certain understanding, also have certain project experience, so some of the more basic knowledge will no longer be record, met some difficult to comprehend, if readers can get to the Internet to find relevant information, if still can’t understand, please contact the author. You are also welcome to point out the mistakes and deficiencies in this article.

Please indicate the source of reprint.

Go language syntax

function

The function definitions

func g(a){}Copy the code

Note that the open curly brace must be on the same line as the function name, otherwise it will not compile.

Function parameters and return values

Functions can take multiple arguments and return multiple values, and the return values can be named.

func getX2AndX3_2(input int) (x2 int, x3 int) {
    x2 = 2 * input
    x3 = 3 * input
    // return x2, x3
    return
}
Copy the code

When a function has multiple return values, as long as one return value is named, the others must be named as well, as in the following code snippet, an error is reported because the return value of the error type is not named.

func funcMui(x,y int)(sum int,error){
	return x+y,nil
}
Copy the code

If more than one value is returned, parentheses () must be added; If there is only one return value, the name must also be parentheses ().

If the last argument to a function uses… Of type, variable length arguments can be passed. If the parameter is stored in a variable slice of type slice, you can use slice… Pass the argument in the form of, call the argument function.

package main

import "fmt"

func main(a) {
	x := min(1.3.2.0)
	fmt.Printf("The minimum is: %d\n", x)
	slice := []int{7.9.3.5.1}
	x = min(slice...)
	fmt.Printf("The minimum in the slice is: %d", x)
}

func min(s ...int) int {
	if len(s)==0 {
		return 0
	}
	min := s[0]
	for _, v := range s {
		if v < min {
			min = v
		}
	}
	return min
}

/* Output:
The minimum is: 0
The minimum in the slice is: 1
*/
Copy the code

defer

Defer is a special keyword in Go that is used to defer the execution order of a function or statement until the function returns, allowing the function to do something on the return, or first out when there are multiple defer statements. The defer statement allows you to close out the process before it aborts, trace code, and record function parameters and return values, as in the following example:

package main

import (
	"io"
	"log"
)

func func1(s string) (n int, err error) {
	defer func(a) {
		log.Printf("func1(%q) = %d, %v", s, n, err)
	}()
	return 7, io.EOF
}

func main(a) {
	func1("Go")}/* Output:
2021/1/2 22:46:11 func1("Go") = 7, EOF
*/
Copy the code

Built-in function

The Go language has built-in functions that can be used without import operations, which can be understood as keywords of some type, but implemented as functions. Here’s a simple list that will be discussed in more detail:

The name of the instructions
close Used for pipeline communication
Len, cap, Len is used to return the length or number of a type (string, array, slice, map, and pipe); Cap stands for capacity and is used to return the maximum capacity of a type (for slices and maps only)
The new, make Both new and make are used to allocate memory: new is used for value types and user-defined types such as custom structures, and make is used for built-in reference types (slices, maps, and pipes). They are used like functions, but with types as arguments: new(type), make(type). New (T) allocates the zero value of type T and returns its address, which is a pointer to type T. It can also be used for primitive types:v := new(int). Make (T) returns the initialized value of type T, so it does more work than new.New () is a function; don’t forget its parentheses
Copy, append Used for copying and joining slices
Panic, recover Both are used for error handling mechanisms
Print and println The underlying print function, the FMT package is recommended in a deployment environment
The complex, real imag Used to create and manipulate complex numbers

To be continued…