Go language Development (i), go language introduction

Source: Internet
Author: User
Tags install go locale mercurial

Go language Development (i), go language introduction First, go language introduction 1, Go language introduction

Go, full name Golang, is a programming language developed by Google that is statically strong-typed, compiled, styled, and has a garbage collection function. Go was developed by Robert Griesemer, Rob Pike, Ken Thompson (C language inventor) in late 2007 and officially announced as an open source project in November 2009 and implemented on Linux and Mac OS x platforms. Subsequent additions to the Windows platform are implemented. In early 2012, the Go language official released the Go 1.0 stable version, the go language is based on 1.x per half-yearly release of a version.

2. The advantages of the Go language

The go language has the following advantages:
A, can be directly compiled into machine code
B, a wealth of standard library support
C, built-in powerful tools
D. Support Cross-platform compilation
E, support automatic garbage collection
F, richer built-in type support
G, support function multiple return value
H, error handling support
I, support anonymous functions and closures
J, support types and interfaces
L, support concurrent programming
M, Support reflection
N, support and C language interaction

3. The disadvantage of Go language

The go language disadvantages are as follows:
A. Lack of framework
B, package management is not perfect

4. Go language use

The main uses of the Go language are as follows:
A, server programming, such as processing logs, data packaging, virtual machine processing, file system, etc.
B, distributed system, database agent, etc.
C, network programming, such as Web applications, API applications, download applications
D, memory database, such as Groupcache, couchbase part of the build
E, cloud Platform, at present, many foreign cloud platform in the use of Go development, cloudfoundy part of the formation of the former Vmare technical director himself out to engage in Apcera cloud platform.

Second, go language environment installation 1, GO language environment download

The go language supports the following systems:
A, Linux
B, FreeBSD
C, Mac OS X (Darwin)
D, Windows
Go Locale installation package:
https://golang.org/dl/
https://golang.google.cn/dl/

Download the appropriate Go locale installation package based on the operating system platform.

2, Linux platform installation go language environment

Extract the downloaded source package to the/usr/local directory
sudo tar -C /usr/local -xzf go1.10.1.linux-amd64.tar.gz
Add the/usr/local/go/bin directory to the PATH environment variable
Export path= $PATH:/usr/local/go/bin

3, Mac platform installation go language environment

The Mac platform can be installed directly by double-clicking the installation package at the end of the. Pkg to install the directory under/usr/local/go/.
brew install go

4. The Windows platform installs the Go language environment

The Windows platform can be installed by using the. msi suffix installation package.
By default, the. msi file is installed in the C:\Go directory, and you can add the C:\Go\bin directory to the PATH environment variable. You will need to restart the Command window to take effect after adding.

Three, go command line tool 1, go command introduction

The go language comes with a complete set of command-handling tools that can be viewed by executing go or go Help on the command line:

2. Go Build

The Go Build command is primarily used for test compilation. During the compilation of a package, if necessary, it is compiled with a dependent package at the same time.
In the case of a normal package, no files are generated after the Go Build command is executed.
In the case of the main package, after the Go Build command is executed, an executable file is generated in the current directory. If you need to generate the appropriate EXE file under $gopath/bin, you need to go to install or use the Go build-o path/a.exe.
If you have multiple files under a folder and just want to compile one of them, you can add a file name after go build, such as go build a.go. The Go Build command compiles all the go files in the current directory by default.
You can specify the file name of the compiled output. For example, you can specify go build-o myapp, which by default is the package name (not the main packet), or the file name (main package) of the first source file.
Go build Ignores "_" or "." In the directory. Start with the go file.
If the source code requires different processing for different operating systems, you can name the file according to the different operating system suffixes. For example, there is a program that reads an array, and there are several source files that may be available for different operating systems:

array_linux.go array_darwin.go array_windows.go array_freebsd.go

The go build will selectively compile files that end with the system name (Linux, Darwin, Windows, Freebsd). Linux System The following compilation will only select the Array_linux.go file, and all other system naming suffix files are ignored.
If used directly, indicates an attempt to compile the current directory corresponding to the code package, if the current directory is not a valid code package (included in the $GOPATH/SRC directory) will be an error, if the code package name, it is to compile the specified code package, if you bring the source file, the compilation of the specified source file.
Go language source files are divided into three categories, including command source files, library source code files and test source files. If you compile a command source file, or a package that contains a command source file (main package), the compilation will generate the executable file, and vice versa, it will not generate any files, just do the check compilation.
You can add some markup when you execute the go build command, such as:
The-v flag, you can print the package name that is built during the execution of the command (including the package that is dependent on the compilation process), and if go build is a source file, the package name will be printed as command-line-arguments, which is the virtual package name generated when compiling the source file. So it doesn't seem strange to see.
The-X flag to print all shell commands used during compilation.
The-o flag, which specifies the path and name of the generated file.
-a flag, forcing recompilation.
The-buildmode=shared tag that specifies the result types that are generated by the current compilation, such as static libraries and dynamic libraries. The Go language uses static compilation by default, and the benefits are simple to deploy, but with dynamic libraries, you can reduce the size of the distribution package. Compiling into a dynamic library is not yet supported under Windows.
If it is used directly, it indicates an attempt to compile the code package for the current directory, if the current directory is not a valid code package (included in the $GOPATH/SRC directory) will be an error, if the code package name, then the specified code package is compiled, if you bring the source file, it will compile the specified source file.

3. Go Clean

The Go Clean command is used to remove the generated files from the current source package, including:
_obj/old Object directory, left by makefiles
_test/old Test directory, left by makefiles
_testmain.go old Gotest file, left by makefiles
Test.out old Test record, left by makefiles
Build.out old Test record, left by makefiles
*. [568ao] Object file, left by makefiles
DIR (. exe) is generated by GO build
Dir.test (. exe) generated by Go test-c
Mainfile (. exe) generated by GO build mainfile.go

4. Go FMT

The Go FMT command is primarily used to format code files.
Development tools typically have a save-time auto-formatting feature, and the underlying is actually called the Go FMT command.
Gofmt-w src, you can format the entire project.

5. Go get

The go get command is primarily used to dynamically retrieve remote code packages and currently supports BitBucket, GitHub, Google code, and Launchpad. The go get command is internally divided into two steps: The first step is to download the source package, and the second step is to execute go install. Download the source package of the Go tool will automatically according to different domain names to invoke different source tools, the corresponding relationship is as follows:

BitBucket (Mercurial Git)GitHub (Git)Google Code Project Hosting (Git, Mercurial, Subversion)Launchpad (Bazaar)

For go get to work properly, you must ensure that the appropriate source management tools are installed and that the source management tool commands are added to the path.
The go get command essentially starts with the source tool clone code into the SRC directory and then executes the go install.

6. Go Install

The Go Install command is internally divided into two steps: The first step is to generate the result file (executable or. a), and the second step is to move the compiled results to the $GOPATH/pkg or $gopath/bin.
. exe file: Typically a go file with the main function is generated, with a function entry, all of which can be run directly.
. A application package: Generally, go install does not contain the main function of the go file generated, there is no function entry, can only be called.

7. Go test

The Go Test command automatically reads the file named *_test.go under the source directory and generates and runs the executable file for the test.
By default, no parameters are required, and the Go Test command automatically tests all test files in the source package, which can be viewed using Go help testflag.
For unit testing, the unit test code is recommended to be in the same code package as the code being tested, with "_test.go" as the latter, and the test function proposes to prefix the name with "test". The Go Test command either tests the code package or specifies that a test code file should be run (with the tested code file)

8. Go doc

The Go Doc command is a very powerful documentation tool for viewing documents such as viewing Builti package execution Go doc builtin, viewing HTTP package execution Go doc net/http. View a function in a package, execute Go Doc fmt printf, or view the corresponding code of the function, execute go doc-src fmt printf.
Execute the godoc-http=: port number, such as godoc-http=:8080, from the command line by command. Then open the 127.0.0.1:8080 in the browser, you will see a golang.org local copy version, through the page can query the PK document and other content. If you set up the GOPATH,PKG classification, the documentation for the standard package is listed, and the documentation for all items in the local gopath.

9. Go fix

Go fix is used to fix the old version of the code to the new version, such as Go1 before the old version of the code is converted to GO1.

10. Go version

The Go Version command is used to view the current version of Go.

11. Go env

The Go Env command is used to view the current go environment variables for printing the Go Language environment information, such as Gopath is the workspace directory, Goroot is the Go Language installation directory, Gobin is through the go The install command generates a directory of executable files (by default, in the bin directory of the current workspace), Goexe to generate the suffix of the executable file.

12. Go List

The Go List command lists the currently installed package.

13. Go Run

The Go Run command is used to compile and run the Go program and can only be used for command source files, which are typically used for rapid development testing.

14. Go Tool

Go tool command Tools, go tool pprof performance Checker,?? Go tool CGO commands related to C and go language.

15. Go Vet

Go vet is a static check tool that is optimized for general projects when they are almost complete.

16. Go Bug

The Go Bug command is used for Go language debugging.

17. Go Generate

Identify the general commands to run by scanning for special comments in the go source. Go generate is not part of go build, go generate does not contain dependency analysis and must be run explicitly before running go build. Go generate is used by the author of the Go package, not its client.

18. Go Help

The Go help xx command is used to view assistance information, such as the Go Help bug, which is used to view go bugs.

Four, go language development environment 1, liteide

Liteide is an open-source, cross-platform, lightweight go Language integrated development environment (IDE).
: http://sourceforge.net/projects/liteide/files/
Source Address: Https://github.com/visualfc/liteide

2, Goland

Goland is an ergonomic new business IDE designed by the Czech JetBrains company to provide go developers. Goland integrates the Code Accessibility and tool integration features of the IntelliJ platform for the Go language, with the following features:
A, encoding auxiliary function
Goland analyzes the developer code and looks for connections between symbols. Provides code hints, quick navigation, flexible error analysis capabilities, and formatting and refactoring capabilities.
B. Ergonomic design
Powerful static code analysis capabilities and ergonomic design make developers more than just working, but also a pleasant programming experience.
C. Integration of tools
Mission-critical tools, such as code coverage tools, full-featured debuggers and versioning are all integrated, without the plug-ins that are randomly installed.
D, IntelliJ plug-in eco-system
In addition to the rich built-in tools, if anything is needed, it can be found in the IntelliJ plugin ecosystem.
Official website: https://www.jetbrains.com/go/
Goland The Go development environment needs to be configured after the installation is complete.

Click the Configure->settings menu and configure the following:

Five, go language example 1, helloworld.go writing
package mainimport "fmt"func main() {   fmt.Println("Hello, World!")}
2. Compile and Execute

go run helloworld.go
Print out: Hello, world!

Go language Development (i), go language introduction

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.