Installation of Go under Linux (Centos), and HelloWorld

Source: Internet
Author: User

Write in front:

At present, the Go language has been released 1.5 version, there are many go language-related books and tutorials, but after looking at some, think or should write a set of Go language tutorials. To the general learning go language friends more than one choice. Because, I wrote the tutorial, has always been different from the other.

Respect Labor, if reproduced, please indicate the Source: Intercity column


The go language is installed under CentOS:

Note that the following commands need to be run as root, or sudo.

Note that if you want to customize the installation location, you need to configure the GOROOT environment variable, so here we use the default directory to install, you can not configure the GOROOT environment variable.

First go to the official website to download the go installation package, here is used: go1.5.1.linux-amd64.tar.gz


1. Select the installation path

Because the default go path, under/usr/local, so use the following command, unzip the creation/usr/local/go

Tar-c/usr/local-xzf go1.5.1.linux-amd64.tar.gz


2, add the PATH environment variable

Vi/etc/profile

Then add the following line:

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


3, set up the Go workspace (workspace, which is the directory that the GOPATH environment variable points to)

The go code must be in the working space. A workspace is a directory that contains three subdirectories:

SRC----Each subdirectory inside, is a package. Baone is the go source file

PKG----generated after compilation, the package's target file

The bin----the generated executable file.

Here, in the/home directory, we create a folder named Gopath (can not be gopath, any name can),

Then create three sub-folders (The subfolder name must be src, pkg, bin). Such as:



4, set Gopath environment variable

Vi/etc/profile

Then add the following line:

Export Gopath=/home/gopath

Once saved, execute the following command to make the environment variable effective immediately:

Source/etc/profile

At this point, the go language environment has been installed. Start HelloWorld below


5, new project (app package).

In general, it is recommended that the package name and directory name be consistent, so, under the SRC folder, organize the folder path according to the package name you want to create.

Here, we build the Hello folder below the/HOME/GOPATH/SRC.

Description

If your package is named Mygo/first/hello, then you need a directory structure like this:/home/gopath/src/mygo/first/hello

If you consider the introduction of Git repository management, you can create such a package name: Github.com/mytest.



6, new Go Code

/home/gopath/src/hello below, build the Helloworld.go file, and edit it, adding the following code:

Package Main

Import "FMT"

Func Main () {

Fmt. Printf ("Hello, world.\n")

}


7, compile and build Go program

Under Any file path, run:

Go Install Hello


You can also enter the path to the project (app package), and then run:

Go Install



Note that when compiling the build Go program, go actually goes to two places to find the package:

Goroot under the SRC folder, and under Gopath under the SRC folder.

In the package, the main function of the main package is automatically used as the entry for the program, and then compiled.


8. Run Go Program

Under/home/gopath/bin/, you will find a hello executable file running with the following command:

./hello


This article is from the "My Ops Time" blog, so be sure to keep this source http://aaronsa.blog.51cto.com/5157083/1893823

Installation of Go under Linux (Centos), and HelloWorld

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.