Go Development on Linux __linux

Source: Internet
Author: User
go development on Linux 1.Go Installation Use 1.1 Download Go source package
$ Curl  -o './go1.6.3.linux-amd64.tar.gz ' https://storage.googleapis.com/golang/go1.6.3.linux-amd64.tar.gz '
1.2 Unzip the folder to the/usr/local directory

Su Root, switch to administrator rights to write files to the/usr/local directory

$ tar zxvf go1.6.3.linux-amd64.tar.gz-c/usr/local/
1.3 Set PATH environment variable, add/usr/local/go/bin to environment variable

Remove the current environment variable $path and use: Add the new environment variable/usr/local/go/bin, assign value to PATH, and finally use export to modify environment variables

$ export path= $PATH:/usr/local/go/bin

Prints the current environment variable and finds that/usr/local/go/bin has been added

$ echo $PATH
1.4 Setting up the workspace (workspace)

Set up workspace home directory

$ mkdir $HOME/go
$ export gopath= $HOME/go
1.5 code Organizational structure

The Go tool is designed to open up the code in the public repository, although you may not need to publish your code, but the settings for the environment are the same.
The go source code must be stored in the workspace. Workspace is a directory (directory hierachy) in which there are three subdirectories src containing go source code files, source code files organized into packages (one package per directory) pkg contains PAC Kage Objects (binary package) bin contains executable command commands (executable binaries)

Go tool compiles (build) source code files (source packages) and installs resulting binaries under the pkg and bin directories.
The SRC directory contains several version-controlled warehouses, such as Git or Mercurial, used to track the development progress of one or more source package. 1.6 Test Demo

To compile and run a simple program, first select a package path (use GITHUB.COM/USR here) to create a package directory (package directory)

$ mkdir $GOPATH/src/github.com/usr/hello-p

Create a file named Hello.go in the package directory, containing the following code

Package main

Import "FMT"

func Main () {
    fmt. Println ("Hello, world.\n")
}

Use the Go tool to build and install this program

$ go Install Github.com/user/hello

Note: You can run this command under any folder on your computer. The Go tool searches for the Github.com/usr/hello package under the $GOPATH specified workspace and compiles and installs the package.
Of course, if you run go install under Package directory, you can ignore package path.

$ cd $GOPATH/github.com/usr/hello
$ go Install

This command builds the Hello command to produce an executable binary file. The executable binaries are then installed under the Bin directory of the working directory, and the filename is called hello (in Hello.ext below windows, you can see that the executable binary file name and directory name are consistent).
When an error occurs, the Go tool only print output, not binary files. If these commands do not produce output, it means they are running successfully.
Now you can run this command by typing the full name of the Hello command.

$ $GOPATH/bin/hello
Hello, world

Or if you have added $GOPATH/bin to the path:

$ Hello
Hello, world
refer to the following content:

Go work space

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.