GO language configuration and basic grammar application (1)

1. Download and installation of golang

 This step is relatively simple. Just open the official website of go and click download. Individuals who open it slowly can use the mirror website in China. After that, the speed of access and downloading of third-party libraries will be much faster, and then the fool-like installation can be done all the way to the end. .

Configure environment variables

Note: If it is an msi installation file, the environment variables of the Go language will be automatically set.

My computer----right click "Properties"----"Advanced System Settings"----"Environment Variables"----"System Variables"

Now assume that Go is installed in the C drive and directory

New:

  • GOROOT : Go installation path (eg: C:\Go)
  • GOPATH: The path of the Go project (for example: E\go). If there are more than one, add them separated by semicolon
  • Path: Add: C:\Go\bin;%GOPATH%\bin to the path. You
    need to configure the executable directory in GOPATH to the environment variable, otherwise the third-party go tool you downloaded yourself cannot be used.
  • The working directory is where we store the source code for development, which corresponds to the GoPATH environment variable in Go. After this environment variable is specified, the files generated by compiling the source code and so on will be placed in this directory. For the configuration of the GOPATH environment variable, refer to the above installation of Go, and configure it in the system variables under Windows.
  • There are three main directories under GOPATH: bin, pkg, src. The bin directory mainly contains executable files; the pkg directory stores compiled library files, mainly *.a files; the src directory mainly stores Go source files.

2. Check whether the installation and configuration are successful.

Use the shortcut key win+R, enter cmd, open the command line prompt, and enter at the command line

go env #View the configuration information of go

go version #View the version number of go 

2. Configure go compiler or IDE

Although it seems that vscode is the most widely used? ? But I am really not used to vscode personally, so I chose goland to get used to that set of operation logic, and it is true that its degree of automation is relatively high hh.

GoLandCreate a project

First, find "New" in the "File" menu, and select "Project" in the lower menu to create a new project.


Select New Project from the menu

Select a directory for the project (try to choose an empty directory), and click "Create" to complete the creation.

create project

Edit run/debug configuration

Every time we create a project, we need to configure Goland, find "Add Configuration" at the top right of Goland and click it.


Click Add Configuration

Click "+" in the pop-up window and select "Go Build" from the drop-down menu.


Edit configuration information

After clicking "Go Build", fill in the corresponding information in the window, as shown below.

The following points need to be noted in the above figure:

  • Name: The name of this configuration information, which can be customized or use the default value of the system;
  • Run kind: This needs to be set to "Directory";
  • Directory: used to set the directory where the main package is located, it cannot be empty;
  • Output directory: It is used to set the storage directory of the executable file generated after compilation, which can be empty, and the executable file will not be generated by default when it is empty;
  • Working directory: It is used to set the running directory of the program, which can be the same as the setting of "Directory", but it cannot be empty.

Tip: Except for the points mentioned above, the rest of the configuration items can use the default values ​​of the system without modification.

write and run code

After configuring Goland, you can write code in the project folder. First create a new Go source file, right-click on the project folder, find "New" in the pop-up menu, and select "Go File" in the sub-menu, and finally enter the file name in the pop-up box (red box ) and select "Empty file", and press "Enter" to confirm.


Create a new Go source file

Write the following code in the newly created Go source file.


write test code

Then, we can click the green triangle on the upper right of Goland to run the program written above.

run the program

 

Guess you like

Origin blog.csdn.net/weixin_52030368/article/details/128681258