Setting Golang environment variables under Linux (Ubuntu)

Source: Internet
Author: User
Tags install go readable mercurial
This is a creation in Article, where the information may have evolved or changed.

1). Source code Installation Go language

Go tool chain in C language, to build it, you need to install a C compiler, this example uses the GCC tool; Entering the Linux terminal, enter the command line

sudo apt-get install build-essential

Go using mercurial for version management, first you must install the mercurial, for Ubuntu/debian system first install easy_install;

sudo apt-get install mercurial

Appears processing triggers for python-support ... The Python environment must be installed first to execute

sudo apt-get install python-setuptools Python-dev

sudo easy_install mercurial

If not, download the mercurial installation directly

Get Go Code

HG Clone-r release https://go.googlecode.com/hg/$GOROOT

Install go:

CD $GOROOT/SRC

./all.bash

If not, then download the compiled code directly to install the decompression go package put the user root directory

sudo tar zxvf ~/downloads/go1.0.3.linux-386.tar.gz-c/opt

2). Set environment variables and add the following to the/etc/profile file

sudo gedit /etc/profileexport GOROOT=$HOME/goexport GOBIN=$GOROOT/binexport GOARCH=386export GOOS=linuxexport GOPATH=$HOME/workspacegoexport PATH=.:$PATH:$GOBIN

Goarch, GOOS for optional configuration, cross-compilation useful Gopath set up a workspace, allow multiple directories, when there are multiple directories, separated by delimiters, when there are multiple Gopath, the default is to put the contents of go get in the first directory;

The $GOPATH directory convention has three subdirectories:

SRC store source code (e.g.,. Go. C. h, etc.)

The files generated by the PKG after compilation (for example:. a)

Executable file generated after bin compilation (for convenience, this directory can be added to the $PATH variable)

Apply the environment variable you just configured: Source/etc/profile

The above configuration is for the system level, and the recommended user-level configuration is as follows:

If the user does not exist under the root directory. Bash_profile, the

cat > .bash_profileexport GOROOT=$HOME/goexport PATH=$PATH:$GOROOT/binexport GOPATH=$HOME/workspacego

Ctrl_d (This is press ctrl-d two keys at the same time)

Every time you start a system application

. ./.bash_profile

The official recommends that the configuration be recorded in the user in ~/.BASHRC or ~/.profile, so that the login can use go instead of, each login system to execute. ./.bash_profile

For the differences in the configuration file, see the following appendix

3). See if Go is a release version

HG Identify

Update go to new version

updaterelease./all.bash

4). Browse Local doc

Executing at the command line

godoc-http=:8080

In the browser address input: http://127.0.0.1:8080, you can enter the Doc interface

5). Build a browser-based interactive go Programming Guide-gotour

5.1). Install the Go Language English tutorial:

sudo go get code.google.com/p/go-tour/gotour

5.2). Install the Go language Chinese tutorial:

sudo go get bitbucket.org/mikespook/go-tour-zh/gotour

In the command line execution Gotour, in the browser address input: http://127.0.0.1:3999, you can enter the tutorial learning interface.

Appendix

Reference: http://blog.csdn.net/wumingxing0228/article/details/6050175

Environment variables are closely related to the shell, and the user logs on to the system and launches a shell. It's usually bash for Linux, but it can be reset or switched to another Shell. For UNIX, it could be cshelll. Environment variables are set by shell commands, and set environment variables can be used by all current users running programs. For bash, the shell program can use variable names to access the appropriate environment variables, and export to set environment variables. This is illustrated by a few examples below.

1) Etc/profile: This file sets the environment information for each user of the system, and the file is executed when the user logs on for the first time. and collect the shell settings from the configuration file of the/ETC/PROFILE.D directory. Note: Here we set the global variables that are available to all users.

2)/ETC/BASHRC: Executes this file for each user running the bash shell. When the bash shell is opened, the file is read.

3) ~/.bashprofile: Each user can use the file to enter the shell information dedicated to their own use, when the user log on, the file is only executed once! By default, he sets some environment variables to execute the user's. bashrc file. Note: ~ Under Linux is the variable representing the home. In addition to different Linux operating systems, this file may be different, may be ~/.bashprofile, ~/.bash_login or ~/.profile one or several, if there are several, then the order of execution is: ~/. Bashprofile, ~/.bashlogin, ~/.profile. For example, I use Ubuntu, my User folder, the default is only the ~/.profile file.

4) ~/.BASHRC: This file contains bash information dedicated to your bash shell, which is read when you log in and every time you open a new shell. (Note: This file is. Beginning, so it is hidden in the folder) so how do we add our own defined environment variables? Open the file with Notepad, and then write it in the last: Xiaokang=kangkang and then save it so that each time we open a new terminal, we have this variable in effect. Remember, if you have opened a terminal, and then you modify the file, then this terminal will not be effective. In general, the user is best to make changes here, but sometimes it will overwrite the parent variables, such as PATH is root set, but if you write in this file path=xx, then all of the future PATH is XX, so we should write in this file: Path= $PATH : xx This adds the original and your own. And note that the Linux system uses: Split to represent side by side, not windo; 3 and 4 are all in the user directory, and their only difference is:. Bash_profile can only be started once when logging in. This 3 file does not seem to be in my Ubuntu.

5) ~/.bash_logout: Executes the file each time it exits the system (exiting the bash shell). In addition, variables set in/etc/profile (global) can be applied to any user, while variables set in ~/.BASHRC, etc. (local) only inherit variables from/etc/profile, they are/"parent/" relationships.

~/.bash_profile is interactive, login way into bash run ~/.BASHRC is the interactive non-login way into bash usually the two settings are roughly the same, so the former usually calls the latter.

OK, let's sum up their execution: when you log in and the shell is bash, bash first executes the command in the/etc/profile file (if the file exists), and then it looks for ~/.bashprofile,~/.bashlogin or ~/in order. Profile file, and executes the command found in the first readable file. When login bash exits, it executes the commands in the ~/.bashlogout file. When you start an interactive bash, it executes the commands in the ~/.BASHRC file (if the file exists and is readable). When you start non-interactively to run a shell script, bash looks for the BASHENV environment variable to determine the name of the execution file.

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.