官术网_书友最值得收藏!

  • Security with Go
  • John Daniel Leon
  • 275字
  • 2021-06-30 19:06:42

Installing the executable file

The go build tool is good for generating an executable file in your current working directory, but there is a way to build and install your applications so that the executables are all collected in the same location.

When you run go install it puts the output file in a default location of $GOPATH/bin. In our case, we set $GOPATH equal to our $HOME. So the default bin directory would be $HOME/bin.

You can override the location by setting the GOBIN environment variable if you want it to install somewhere else. To install our hello program, we will run the following command:

go install hello

This will build and create an executable file, ~/bin/hello. The bin directory will get created automatically if it does not already exist. If you run the install command multiple times, it will rebuild and overwrite the executable in the bin directory. Then the application can be run with this:

~/bin/hello

You can add ~/bin to your PATH environment variable for convenience. Doing so will allow you to run the applications from any working directory. To add the bin directory to your PATH, run this in the Terminal:

echo "export PATH=$PATH:$HOME/gospace/bin" >> ~/.bashrc

Be sure to restart your Terminal after that to refresh the environment variables. After that you can run the hello application by simply typing the following into the Terminal:

hello

Installing the application is completely optional. You don't have to install programs to run or build them. You can always build and run from your current working directory when developing, but it can be convenient to install finished applications that get used.

主站蜘蛛池模板: 信阳市| 墨脱县| 武威市| 衡阳市| 应城市| 康保县| 临夏县| 大足县| 邹城市| 兴山县| 霍山县| 玛曲县| 普格县| 怀宁县| 苏尼特左旗| 彝良县| 长垣县| 故城县| 桑植县| 河北区| 保康县| 光泽县| 青海省| 桦南县| 衡阳市| 民乐县| 安新县| 邹城市| 正宁县| 赞皇县| 克山县| 安阳市| 东兴市| 峨边| 锡林郭勒盟| 龙口市| 旅游| 阳城县| 水城县| 自治县| 会宁县|