Go Language Primer "one": Installation and Environment configuration

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

This article mainly introduces the ecology of Go. Includes installation, Ide,go command three parts. As the author continues to learn, content may be updated.

Installation

As of this writing, the go version has been updated to 1.7.3. Generally speaking, install go only need to go to the https://golang.org/dl/official website to download the corresponding package installation. Mac users can also pass the homebrew, as for Ubuntu users can also be Apt-get way, which is consistent with the usual software.

After the installation is complete, in console input go version , the console returns a similar
go version go1.7.1 darwin/amd64That means success.

After verification, several environment variables need to be configured by the developer:
For MacOS, the author is configured in ~/.ZSHRC (installed by the installation package downloaded via the official website):

export GOROOT=/usr/local/goexport GOPATH=$HOME/goworkspaceexport PATH=$PATH:$GOROOT/bin:$GOPATH/bin

goroot: Similar to Java_home in Java, configuration to go installation directory, depending on the installation mode and operating system, the location is different.
Gopath: This variable is not common in other languages, it is equivalent to workspace, to store the code, the packaged module, the executable file. After the configuration with the Go command described later is very convenient, you can see that the go language is an engineering-oriented language. Just customize a directory here.
The inclusion of these two paths in path means that the go-generated executable can be executed directly from anywhere.

At this point, the installation work is all over.

Development tools

In general, the go development tool is relatively weak in Java, mainly:

    • Liteide
    • Sublime
    • Vim
    • Eclipse
    • IntelliJ idea

5 options, only with liteide can be more convenient debugging, other tools need to configure GDB, and not specifically for go write IDE. The current version of Liteide is also relatively rough, the author chose Sublime.


Add Gosublime via Packagecontrol

After installing the Gosublime plugin, you can perform code jumps and code hints. Debugging basically depends on print, as a way to familiarize yourself with your language. Detailed IDE installation steps are suggested by Google separately.

Go command

Go command is a very special part of go, through the GO command, it is easy to implement other languages need third-party tools to do some of the features (such as package management).
Let's start with a summary:

  Go is a tool for managing go source code.           Usage:go command [arguments]the commands are:build compile packages and dependencies Clean Remove object files Doc show documentation for package or symbol ENV Prin T go Environment information fix run Go tool fix on packages FMT run GOFMT on package Sources generate generate Go files by processing source get download and install packages a           nd dependencies install compile and install packages and Dependencies list List packages Run compile and run Go Program test test Packages tool run specified Go Tool version print Go version vet run Go tool vet on packages  

This is all supported commands. Common explanations are as follows:
build: Compiles a. Go format source file into an executable file or module, depending on the package in the code, and if it is package main, it is generally compiled into an executable file and generated into the current directory.

Install: Build at the same time, installed to (that is, copied to) the Gopath/bin directory, so that can be executed anywhere.

get: equivalent to Maven in Java, gradle do things, such as the use of a third-party package in the code github.com/go-sql-driver/mysql , we need to execute before the build go get github.com/go-sql-driver/mysql . The system will download the code GOPATH/src/github.com/go-sql-driver/mysql below the directory structure, which is a convention. After that, the code contained in the package can be compiled with the build command. From here you can see that the go is highly free package management, while the open source is very friendly, engineering convenience no redundant operation.

FMT: After executing go fmt xxx.go, the code style of the target file will be unified, such as curly braces {} Not another line, delete extra empty lines, etc. It's good to unify everyone's coding style in collaboration.

run: Runs a. Go file directly and does not generate an executable file. Such asgo run test.go

Doc: The doc command is now replaced with the Godoc command, which requires the installation of Godoc:

go get -v  golang.org/x/tools/cmd/godoc

You may need to turn over the wall and install it to become a handy tool for viewing code documents. such as querying FMT. println function Usage:

[~]$ godoc fmt Printlnuse 'godoc cmd/fmt' for documentation on the fmt commandfunc Println(a ...interface{}) (n int, err error)    Println formats using the default formats for its operands and writes to    standard output. Spaces are always added between operands and a newline    is appended. It returns the number of bytes written and any write error    encountered.

In addition, the command line is executed with godoc-http=: port number such as godoc-http=:8080. Then open the 127.0.0.1:8080 in the browser and you'll see a golang.org local copy version, which is really addictive.

A few other commands:
Go version View the current release of Go
Go env View the current GO environment variables
Go test executes this command and automatically reads the file named *_test.go under the source directory, generating and running the executable file for the test.
Go list lists all currently installed package

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.