Go environment variable configuration (goroot and Gopath)

Source: Internet
Author: User
Tags mkdir

Goroot is the Go installation path
Add the following statement in the ~/.bash_profile:

Goroot=/usr/local/go
Export Goroot

Of course, to execute the GO command and Go tool, you need to configure the path of the go executable file:
The operation is as follows:
The configuration in ~/.bash_profile is as follows:
Export $PATH: $GOROOT/bin
If Windows needs to be used; symbol split two paths, Mac and Unix-like are used: symbol segmentation

Gopath:go Install/go get and go tools are used to gopath environment variables. Gopath is the search path as the compiled binary storage destination and import package (in fact your working directory, you can create your own go source file under SRC and then start working). Gopath consists of three main directories: bin, pkg, src bin directory mainly store executable files; Pkg directory to store compiled library files, mainly *.a files; SRC Directory is the main source of Go files do not set the Gopath to go installation path,
You can create a directory yourself under the user directory, such as Gopath
The operation is as follows:

CD ~
mkdir Gopath
Add the following statement in the ~/.bash_profile:
Gopath=/users/username/gopath Gopath can be a directory list, go get download third-party library, will generally download to the first directory of the list needs to be gopath in the executable directory is also configured in the environment variables, Otherwise, the third-party go tool that you download yourself cannot be used, as follows:
Configured in ~/bash_profile,
Export $PATH: $GOPATH/bin
Create a Go project and compile and run:
mkdir goproject
cd goproject
touch hello.go
In the Hello.go, enter:
Package main
Import "FMT"
func Main () {
   fmt. Println ("Hello, GO!")
}
Execute the Go build command under the project root to build your project, and the build will generate a hello file
Run the generated file./hello, terminal output: Hello, GO!
Of course you can also run the command go running hello.go directly to execute the program.

If you want the above settings to take effect, you can execute the command: source ~/.bash_profile, all of the above actions are operating under the Mac system, and if it is a non-MAC system, make your own work.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.