Go Language Introduction (i) features, installation, environmental construction, the first program, etc.

Source: Internet
Author: User
Tags install go mercurial gdb debugger



The go language is a new programming language launched by Google that can reduce the complexity of your code without compromising application performance. Rob Pike, Google's Chief software engineer, said: "We've developed go because software development has been frustrating over the last more than 10 years."


Go language Features
    1. Automatic garbage collection
    • Memory is automatically recycled and no developers need to manage memory
    • Just need new to allocate memory, no need to release
    1. Has a richer built-in type


The go language supports a number of other advanced types, such as dictionary type map, array slicing (Slice), in addition to the simple built-in types supported by almost all languages, such as Integer and float.


    1. Supports multiple return values for functions


In c,c++, including some other high-level languages, multiple function return values are not supported. But this feature is really needed, so in c it is generally done by defining the return value as a struct, or by returning it in the form of a function's argument reference. In the go language, as a new language, the target localization for the powerful language of course can not give up the satisfaction of this demand, so support function multiple return value is necessary.


    1. Supports high concurrency


The go language introduces the Goroutine concept, which makes concurrent programming very simple. The go language makes concurrent programming lighter and more secure by using the concurrency mechanism of goroutine instead of the bare operating system, and by using message passing to share memory instead of using shared memory to communicate. By using the keyword go before a function call, we can let the function execute in goroutine mode, Goroutine is a more lightweight and resource-saving process than threading.


    1. Error handling


In the traditional OOP programming, in order to catch the robustness of the program needs to catch the exception, most of the methods used are try () catch{} module, for example, in the following Java code, you may want to do:


 
 
1 try{
2     Statement stmt = ...;
3 }
4 catch(...){
5     ...
6 }
7 finally{
8     conn.close();
9 }


In go, three keywords are introduced, namely defer, panic, and recover, where the use of the DEFER keyword statement means that the code is executed automatically when the function exits, regardless of whether the program has an exception.
So above your Java code with Go process rewrite only two lines:


 
 
1 conn := ...
2 defer conn.Close()




Go Language Installation


The Go language supports the following systems:


    • Linux
    • Freebsd
    • Mac OS X (also known as Darwin)
    • Window


The installation package is: https://golang.org/dl/






After the Go language installation, the C:\Go directory has a total of 9 directories and 9 files, such as:






API -directory containing all API lists for easy access by the IDE



bin-directory, storing compiled executable files



Blog-Directory,



Doc-directory, help documentation



Lib-directory,



Misc-Catalogue,



pkg-directory, which holds the compiled package file. The files in the pkg are generated by Go compilation



src-directory, storing project source files



Note: In general, bin and pkg directories can not be created, the GO command will be created automatically (such as Go install), only need to create the SRC directory.





Common Commands


Go Build



The Go Build command compiles the source code files or packages we specify and their dependencies.



Go build-o bin/tongbupan Go_dev/tongbupan/main



-O: Specifies the directory and name after compilation






Go Install



The Go Install command is actually divided into two steps internally: 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.






Go run



The Go Run command compiles and runs the command source file.



Note that it does not generate an. exe file and only produces a temporary file (but) that executes the results directly at the command-line output to facilitate user debugging.






Go get



Go get can download or update the specified code packages and their dependent packages from the Internet on demand and in real-time, and compile and install them.






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.






Go clean



The Go Clean command removes some files and directories that are generated when executing other commands






Go Doc & Godoc



The Go Doc command can print documents attached to the Go Language program entity. We can use the identifier of the program entity as the parameter of the command to achieve the purpose of viewing its document.



Godoc is a powerful tool that also shows the documentation for the specified code package.






Other commands



The Go language also provides other useful tools, such as:



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



Go version View the current release of Go



Go env View the current GO environment variables



Go list lists all currently installed package





Go language development tools


Goland



Jetbrain's Goland is a powerful IDE, but only 30 days of free trial.






Liteide



Liteide is a simple open source IDE. It is noteworthy that it is the first IDE released by the Go Language 2012 release, developed by QT and looks like other compilers such as Visual Studio and GCC C + +.



Because it is designed directly for Golang, Liteide provides developers with many useful features, including configurable build commands, advanced code editors, and extensive golang support. Other features include code management, GDB and delve debugger, automatic completion and use of Wordapi themes, MIME type based systems, and more.






VS Code



It is a popular open source IDE developed by Microsoft and has an out-of-the-box go extension available for use with VS code code. The Vscode-go plugin provides developers with more features, including integration with many go tools.



VS Code provides smart completion functionality through IntelliSense, built-in Git integration, and direct debugging of code from the editor. VS code is highly scalable and offers many customization options through many of its extensions. It also provides support in dozens of languages, making it a popular tool for developers.






Eclipse Plugin Goclipse



Goclipse is a plugin for eclipse. Using the Goclipse plugin, developers can program with the popular Eclipse IDE. Both the Eclipse IDE and the Goclipse plugin are free and open source. The Goclipse Editor provides a wide range of features for developers, including source code editors, Project wizards, and builders to help report errors built in the editor, as well as full-featured GDB debugger support.





Development Environment Construction


Windows:



Download Go1.6.windows-amd64.msi installation file, install location select default C:\Go\



Configure environment variable Path:c:\go\bin after installation.



Then create a gopath environment variable, this variable is very important, I write the code to be placed in this variable in the configuration of the directory, the go compiler will find and compile;



Continue to create a goroot variable, with the Go compiler to install the target goroot:c:\go\;



After completing the above steps, open the command line Input # Go version, the Go compiler version number will appear.



Enter # Go env to see the right go environment.






Linux:



Linux deployment Golang, first of all, install the SSH Remote Tools on windows, such as XSHELL4.



1. Install mercurial package (mercurial version management system, can output HG name detection is installed)



sudo apt-get install mercurial or # sudo easy_install mercurial



2. Install git



sudo apt-get install git



3. Installing GCC



sudo apt-get install gcc



4. Download the Golang compression pack



wget https://storage.googleapis.com/golang/go1.6.linux-amd64.tar.gz



TAR-ZXVF Go1.6.linux-amd64.tar.gz-c/Home



Rename the unpacked folder to go



Add environment variable: # Vi/etc/profile



Add the following content to the profile



Export Goroot=/home/go



Export path= $GOROOT/bin: $PATH



Export gopath=/home/gopkg



Gopath and Goroot will be changed to your path.



Then we refresh the environment variable: #source/etc/profile



Finally, we verify that the installation was successful: # Go version



5. We can also install without downloading Golang's compression package



sudo apt-get install Golang



Go version






Mac:



Download go1.6.darwin-amd64.pkg installation file for installation



Or, after installing homebrew, enter the command:



Brew Update



Brew Install Mercurial



Brew Install Go



For installation.



Enter # Go version and the Go compiler will appear.



Enter # Go env to see the right go environment.





First Go program


Not mundane, the same as a Hello world.


 
1 package main
2 
3 import (
4     "fmt"
5 )
6 
7 func main() {
8     fmt.Println("Hello World")
9 }





Save the above code to "C:\mygo\src\hello.go", then open the command line, execute: Go install Hello, you can see the output: Hello World





Project Directory





Bin: Store the compiled executable file



PKG: Store the compiled package file



SRC: Storing project source files






Go Language Introduction (i) features, installation, environmental construction, the first program, etc.


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.