Environment construction of Goland under Windows

1. Download the installation package

Enter the Go official download interface to
Insert picture description here
download and install. When installing, be careful not to install it on the C drive. I will install it on the D drive here;

2. Environment variable configuration

The installation package will automatically add environment variables, but the default Go workspace is C drive, and we generally don't put our own files on the C drive. Here is my workspace as an example 我的Go项目文件放在D盘的GoFiles文件夹中, so here is a demonstration of reconfiguration;

Mainly to configure two environment variables: GOROOT and GOPATH;
GOROOT refers to the installation directory of GO.
GOPATH is used as the storage destination of the compiled binary and the search path when importing the package. Do not set it as the installation directory of GO. We generally use it for GO The path of the project;

2.1 Configure GOROOT

In the system variables, you can see GOROOT , the path value is defaulted to C:\administrator\Go 或者 C:\Go, and we installed it on the D drive, then it should be changed here. D:\Go
Insert picture description here
In the system variables, you can see Path (consisting of multiple values), make sure it contains: %GOROOT%\bin This value (equivalent to D:\Go\bin)
Insert picture description here

2.2 Configure GOPATH

Start Goland and create a new project testProject; the
Insert picture description here
folder I temporarily use to store the Go project is D:\GoFiles\testProject, so set the GOPATH of the user variable to this path;
Insert picture description here
then create a new bin folder in the testProject project folder just created;
then in the user variable Add to the path%GOPATH%\bin
Insert picture description here

2.3 Testing environment

Input in cmd go env
Insert picture description here
means the configuration is complete;

3. Test output

Under the project testProject folder, I created a new src folder, which is the default usage of go. Generally, the project code is placed in the src folder ;
(all the third-party package paths referenced in the code, the prefix defaults to "project name/src/third-party package path"; for example, the third-party package is referenced in main.go: import "Golang.org/x/net/websocket", then the package must be located in the "testProject/src/" folder, otherwise the dependent package will not be found and compilation error will occur;)
Create a new Go file test1 in the src folder, Then enter the following code inside:

package main

import (
	"fmt"
	_ "fmt"
)

func main()  {
    
    
	fmt.Println("hello world!")

}

Click Add Configuration in the
Insert picture description here
Insert picture description here
Insert picture description here
above figure, there are the following points to note:

  • Name: The name of this piece of configuration information, which can be customized or the system default value can be used;
  • Run kind: It needs to be set to "Directory" here;
  • 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. It can be empty. When it is empty, the executable file will not be generated by default;
  • Working directory: Used to set the working directory of the program, which can be the same as the setting of "Directory", but it cannot be empty.

Then click to run;
Insert picture description here
refer to the article:
golang win10 environment configuration
Go study notes 01-Environment
Setup Goland Getting Started Guide (use Goland to create and run the project)

Guess you like

Origin blog.csdn.net/wyll19980812/article/details/109043814