Installation and code organization of Golang

Source: Internet
Author: User
Tags learn golang
This is a creation in Article, where the information may have evolved or changed.

Since the first time in the Turing community to see Xu Xiwei's "Go language programming" know that there are golang this programming language has been nearly a year, since then began to learn Golang,golang community is very active, I think Golang is the future trend.


Well, here's a brief introduction to Golang's installation today.


First, go to https://code.google.com/p/go/downloads/list download the latest Go tools


Second, download the latest compression package extracted to your Golang directory, and then set the environment variable:

1) Goroot: Set your goroot,goroot is the root directory of your go tree, this is optional, you can set this variable when you want to switch between multiple local repository replicas

goroot=. /.. /go


2) Add%goroot%/bin to the path variable so that you can use GO command


3) Gopath:go command here to find

Gopath=.../.../mygo

Gopath is your working directory, the following includes the BIN,PKG,SRC three directories, the bin is stored in the executable command, PKG is stored in the compiled package object (suffix ". a"), SRC is stored in the source code

Gopath

Bin

Pkg

Src

You can also add%gopath%/bin to the path so that you can execute your own executable file in the command line.


Third, test the installation is successful

Create a new hello.go under SRC:

Package Main


Import ("FMT")


Func Main () {

Fmt. Println ("hello,world!")

}

Save and execute go run hello.go on the command line, if print out "hello,world!" The description OK


Iii. Build and Install

Here we have to look at the Golang code structure, after installation you can write in the SRC under the new folder (package), such as Hello, go to this folder to create a new go file, can be the above very simple hello.go,

Create file: $GOPATH/src/hello/hello.go

Package Hello

Func sayhi (name string) string {
Return "Hi," + name + "!"

}


Go to $gopath/src/hello, execute go installl or go directly to install $GOPATH/src/hello

After successful execution, the working directory structure is as follows:

pkg/

windows_386/#OS

Hello.a #package Object

src/

hello/

Hello.go #package Source


Golang can use the code in package main as an executable command and install it into the $gopath/bin directory

Create $gopath/src/test/sayhi.go

Package Main

Import (
"Hello"
"OS"
"FMT"
)

Func Main () {
If Len (OS. Args) = = 2 {
Fmt. Println (hello. Sayhi (OS. ARGS[1]))
}else {
Fmt. Println (hello. Sayhi ("Jack"))
}

}

Installation:

>go Install $GOPATH/test

Run:

> $GOPATH/test John Doe

Hi, John Doe!

> $GOPATH/test

hi,jack!


At this point the working directory structure is as follows:

bin/

Test

pkg/

windows_386/#OS

Hello.a #package Object

src/

hello/

Hello.go #package Source

test/

Sayhi.go


Well, here is basically the installation of Golang and the structure of the code, and then talk about other topics Golang.

Finally, it is important to note that if your platform is windows, the command line may not display correctly in Chinese, you need to change the code page to 65001, do chcp 65001, and your editor encoding to be set to UTF-8 when saving the source file.


----If there are any errors, please indicate----




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.