Go installation and configuration

The installation

[^ Download address] : golang.google.cn/dl/

Once the download is complete, run the.msi file and follow the instructions to install.

Environment variables for GO are automatically configured.

After the installation is complete, press Win+R and enter CMD to open the command prompt

Go Version # Check the go version

At this point Go Ann succeeded

Go env # Check environment variables for go

Configure golang development environment in VScode

Vscode could have been installed automatically, but the installation failed due to the network problem, which wasted a lot of time. The main is to set up Go agent, and then Go to vscode to install.

# enable go mod go env - w GO111MODULE = on # set go Proxy agent go env - w GOPROXY = https://goproxy.io, directCopy the code

Detailed tutorial juejin.cn/post/686936…

Vscode resolves Chinese garbled characters on terminals

The main is to set the terminal to UTF-8 encoding, the tutorial is as follows

www.shuzhiduo.com/A/6pdD20AkJ…

Go program minimum structure

Hello World example

The Go program consists of the following parts: package declaration import package function variable statements and expression comments

Let’s take a look at the simple code that prints the word “Hello World.

package main

import "fmt"

func main() {
   /* This is my first sample program. */
   fmt.Println("Hello, World!")
}
Copy the code
  • The first line of package main defines the package name in which the program resides. This is a mandatory statement because the Go program runs in a package. The main package is the starting point for running the program. Each package has an associated path and name.
  • The “FMT” imported in the next line is a preprocessor command that tells the Go compiler to include the files in the FMT package.
  • The next line of function main() is the main function that the program starts executing.
  • The next line /… / is ignored by the compiler and comments are added to the program.
  • The next line FMT. Println (…). Is another function in Go that displays the message “Hello, World!” on the screen. . Here the FMT package has exported the Println method to display the message on the screen.
  • Notice the capital P of Println. In the Go language, the name is exported if it begins with a capital letter. Exporting means that functions or variables/constants can be accessed by the importer of the corresponding package.

Execute the Go program

Open a text editor and add the above code. Save the file as Hello. go and open the command prompt. Go to the directory where you saved the file. Type Go run hello. Run the code and press Enter. If there are no errors in your code, you will see “Hello World!” Print it on the screen.

$ go run hello.go
Hello, World!
Copy the code