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 installation

Installation under Windows

Since the main operating system used by this writer is Still Windows (no money for a Mac), the first attempt was to install GO on Windows.

GO to the official download address of GO, click the download link under Microsoft Windows to automatically download the latest GO stable version installer. Run Install to complete the installation.

If you have installed a Package manager under Windows, such as Scoop and Chocolatey, you can also use the command:

scoop install go
Copy the code

or

choco install go
Copy the code

Install.

Installation under Linux

The current Linux distributions used by the author in this paper are Ubuntu 20.04 and Deepin 20 under the Debian system, so the commands used are operations under the Debian system.

Open the terminal and type

sudo apt install golang
Copy the code

The installation is complete.

GO language IDE

A good IDE makes writing code much easier. The IDE used by the author of this paper is GoLand launched by JetBrains. Usually, I will use Visual Studio Code and Go language plug-in to write small codes.

The download, installation and configuration of the two software is for readers to learn by themselves and will not be described in this article.

The syntax of GO

As the C language of the 21st century, Go language has many similar grammatical features with C language. For example, it is necessary to declare the type of each variable. This paper does not explain the basic details of Go language syntax too much. In this paper, only the author encountered in the process of learning more difficult or complex concepts to make a note of the explanation.

Variable and constant declarations

Go constant declarations use the const keyword: const identifier [type] = value

The variable declaration of Go uses the var keyword: var Identifier Type

One of the biggest differences between Go’s variable declarations and those of other languages is that the type is placed after the variable name. This will be used everywhere and is the first hurdle for beginners to learn Go. Here are a few examples of variables declared in Go:

var a int
var b,c *int
var d bool
// The factorization keyword is used for global variables
var (
	str string
	array []int
)
Copy the code

As you can see, this declaration makes the variables’ types clearly defined. Each line must be of the same type.

Variable naming in Go follows camel nomenclature, such as myArray and startDate.

Go variables are given initial values of the type when declared: int 0, float 0.0, bool false, string empty string, pointer nil. All variables in Go are initialized. The Go language also supports initialization of variables at declaration time. At the same time, if the type is not specified during initialization, Go automatically determines the type based on the initialized value, but variable declarations without an initialized value and type are not allowed.

Due to the automatic judgment feature, Go supports a short declaration syntax :=, such as a := 1, at which point A is automatically initialized to a variable of type int with a value of 1. (This can only be used once for the same variable name within a code block.)

The Go language also supports simultaneous declarations or assignments of multiple variables

a, b, c := 5.7."abc"	// The variable was not previously declared
d, e, f = 1.3."def"	// The variable was previously declared
a, b = b, a	// Swap the values of two variables
_, b = 5.7	// discard values using the write-only variable "_"
Copy the code

Finally, there is support for including an init function in each source file, which supports initialization of global variables. This is a very special class of functions that cannot be called manually, but are executed automatically after each package is initialized and takes precedence over the main function. The following example initializes Pi to 3.1415

var Pi float64

func init(a) {
   Pi = 3.1415
}
Copy the code

string

Similar to Python strings, + concatenation strings are supported, len(STR) is supported to get string length, and STR [] is supported to get content. More features are supported if a Strings package or strconv package is imported. The related functions are described in detail by referring to this link.

Pointer to the

The Go language has Pointers similar to C, but Pointers in the Go language do not allow pointer algorithms, such as pointer+2 or Pointer ++.

Pointers to Go are automatically backreferenced, such as

v := 1
var p *int
p = &v
fmt.printLn("v: %d p: %d p: %p\n",v, p, p)	//Output: V: 1 p: 1 p: 0x24f0820
Copy the code

If – else structure

The standard structure is as follows

if condition1 {
	// do something	
} else if condition2 {
	// do something else	
} else {
	// catch-all or default
}
Copy the code

The switch structure

The switch structure in Go language can receive expressions in any form, or even use statements to judge directly in case without receiving expressions (unlike C language, which can receive integer data), and it does not need to use break statements to end. When the following code needs to continue to be executed, You can use the fallthrough keyword to do this.

switch a, b := x[i], y[j]; {
	case a < b: t = - 1
	case a == b: t = 0
	case a > b: fallthrough
    case default: t = 1
}
Copy the code

In the code snippet above, variables A and B are initialized in parallel and then used as a judgment condition.

For the structure

The basic usage is similar to C, except that the parentheses are not needed.

For is the only loop structure that the Go language provides. Go does not have a while or do-while loop because both can be implemented as a for loop.

Go also supports a special iterative construct for-range, of the general form: for ix, val := range coll {}. Where val is a copy of the corresponding value in the collection, and modifying it does not change the value in the original collection.

To be continued…