Configure, install and run go programs on Ubuntu 12.04 64bit

Source: Internet
Author: User
Tags install go
This is a creation in Article, where the information may have evolved or changed. Attention:
The following installation configuration is in accordance with the official website or textbook <go language programming > part of the content.

I. Download and install Go
On Ubuntu12.04, if you are installing using the Go installation package, simply enter the command line
sudo apt-get install golang-go
Below we focus on the more universal source installation, which is applicable in Linux distributions.

Go to go Language official website http://golang.org/
Depending on the operating system and CPU architecture to choose the Go version to install, I am using Ubuntu 12.04 64bit, so I will download the AMD64 version under Linux:
If an older version is upgraded, it is recommended that you remove and then download and install a new version of the version
wget https://go.googlecode.com/files/go1.2.linux-amd64.tar.gz

Installing to the System directory
After downloading the archive, extract it to the system directory/usr/local below, generate a go tree under/usr/local/go, such as
Tar zxvf go1.2.linux-amd64.tar.gz-c/usr/local
It is recommended to use the root user to set the persistent environment variable
Vim/etc/profile
Join at the bottom of the line
Export Path=/usr/local/go/bin: $PATH
Save Exit and update
. /etc/profile
You can use the following command to verify that the environment variable you just set is in effect?
Echo $PATH
Note: This approach requires you to log in with the root user all the way, including the Go workspace directory under the root directory.

Installing to a custom directory
If you only use a remote Linux server for a normal user, no root privileges, then you can only install go under your home directory, you must set the GOROOT environment variable
For example, if you install go to your home directory, you need to set the persistent environment variable
Vim ~/.BASHRC
Join in the last line
Export Goroot=/usr/local/go
Export path= $PATH: $GOROOT/bin
Save Exit and update
. ~/.bashrc
Note: The path to the $goroot/bin is added here, under PATH environment variable, to execute commands such as go,godoc,gofmt directly under all paths, see below:



Two. Uninstall go
If you need to uninstall go, you need to remove the Go directory installed in the system and remove the go-related environment variables from/etc/profile or ~/.BASHRC, and update the profile synchronously.

Three. Verifying the Go Installation
Method One: Run directly below the current command line
Go version
If you can display the version information correctly, the installation configuration of Go is basically correct


Method Two
(This example is not recommended because it involves the setting of the workspace, see later analysis)
Write a simple Hello World program hello.go to verify:
Package Main
Import "FMT"

Func Main () {
Fmt. Printf ("Hello, world\n")
}
Compiling and running using the Go tool
$ go Run hello.go
Hello, world
If the results are displayed correctly, the environment settings for Go are correct

Four. Set up working environment setup your work environment
Let's assume that go has been successfully built on my work machine.
To build a Go program, two steps are necessary: Compile and link, all of which are done by the Go build build tool. But the go build tool can build local programs and local packages to meet 3 conditions:
(1) The bin directory of Go must be in the environment variable, which is guaranteed by the go on top of our configuration.
(2) There must be a directory tree containing SRC, which contains the source code of the local program and the local package
(3) The previous level of SRC must be in the environment variable Gopath

The Go tool requires you to organize your code in a special way, that is, go code must be placed in a workspace, workspace is a multiple nested directory structure (hierarchy), and its root directory has 3 directories:
SRC--Contains the source files of go, organized into a package, one directory per packet
PKG--Contains the package object
Bin--Contains executable files
Go tool builds the package in SRC, installs the resulting binaries into the pkg and bin directory below

The GOPATH environment variable specifies the location of your workspace, which is probably the only environment variable you need to set up when you develop the go code, and you need to create a workspace directory and set it to the GOPATH environment variable before starting source development. The workspace directory can be placed anywhere on the system, as long as it is not the same as the Go installation directory.

In order to develop our go programs and packages in multiple directories, we need to set the environment variable as follows
Mkdir-p ~/program_develop/go_demo
Vim ~/.BASHRC
Export Gopath=/home/taoyx/program_develop/goeg:/home/taoyx/program_develop/go_demo
Export path= $PATH: $GOPATH/bin
Save exit, and update
. ~/.bashrc
With the above settings, the Go program we produce can be executed under any path in the system.

Five. Download the relevant source code for the book
From the book official website http://www.qtrac.eu/gobook.html download the corresponding source code, the use of chrome download can be.
Unzip to the specified directory:
cd Download '
Unzip gobook-1.0.zip-d ~/program_develop/
CD ~/program_develop/goeg

The following verifies whether a sample program can run?
CD Src/hello
Go Build
The resulting executable file is run directly under the current directory.
./hello

Another way: The resulting executable file is under the bin directory of workspace, not the current directory
Go Install
CD ~/program_develop/goeg/bin
./hello

Six. View the documents in a package locally
Use the Godoc command in the terminal to view the entire package or a document for a particular feature in the package, such as
Execute at Terminal
Godoc Image Newrgba
An image of the function will be output. Documentation for NEWRGBA ()
Godoc Image/png
Will output documents about the entire Image/png package

Seven. View the official go language document locally
The terminal now runs the Godoc tool, providing a parameter for the Godoc to run as a Web server
godoc-http=:8000
The port number can be specified here, as long as it does not conflict with the server port number that is already running, and then opens in the Chrome browser.
Http://localhost:8000/
You can view the official go language documents locally, which is very similar to the homepage of golang.org. See below:



Eight. Reference documents:
Go language Programming, Xu Xiwei, Lu Guihua and other translations
http://golang.org/#
Https://code.google.com/p/go/downloads/list
Http://golang.org/doc/install

Http://golang.org/doc/code.html


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.