Home  >  Article  >  Backend Development  >  Go language generates executable files through commands

Go language generates executable files through commands

尚
forward
2020-06-18 17:27:133210browse

Go language generates executable files through commands

After installing go, we usually set the GOROOT and GOPATH environment variables. However, sometimes due to the complex project structure in actual work, when the set GOPATH cannot meet the needs, it can be set in cmd Temporary GOPATH;

Many IDEs, such as IDEA, can also set global GOPATH and temporary GOPATH, but compiling executable files may be complicated or the app.conf configuration file cannot be loaded when compiled or run through the IDE. situation, I have encountered this pitfall.

So the go executable file is generated through commands.

  • For example, the project structure is like this:

Go language generates executable files through commands

Since it depends on beego in github.com, we need to add github.com Parent directory to GOPATH.

1. Open the cmd command window and use the command to set the environment variable for the path of the package to be compiled and the path of the dependent package, that is, GOPATH (this setting only takes effect for this window):

set GOPATH=E :ProgrammerRouteGoDevelopment\

2. Then set the operating system:

  • Generate the windows executable file:
set GOOS=windows
  • Generate linux executable file:
set GOOS=linux

3. Then execute go install

go install sayHello

in the src directory if no error is reported , the bin and pkg directories will be generated under GOPATH, and the executable file is in the bin directory, as shown in the figure:

Go language generates executable files through commands

Use versions after beego1.7.0 to run the go project with the IDE The configuration of app.conf cannot be loaded, nor can it be loaded using go install;

You can use go run main.go to load app.conf, and you can also use the bee tool to load it.

For more related knowledge, please pay attention to the go language tutorial column

The above is the detailed content of Go language generates executable files through commands. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete