Go Language Command line operation command details

Source: Internet
Author: User
Tags builtin mercurial

Transferred from: http://www.jb51.net/article/56781.htm

Go command

The go language comes with a complete set of command-manipulation tools that you can view by executing go on the command line:

Figure Go command to display detailed information

These commands are very useful for the code that we normally write, so let's look at some common commands.

Go Build

This command is primarily used for test compilation. During the compilation of a package, the package associated with it is compiled, if necessary.

1. If it is a normal package, just like the MyMath package we wrote in section 1.2, when you execute go build, it will not produce any files. If you need to generate the appropriate files under $gopath/pkg, go install is required.

2. If it is a main package, when you execute go build, it will generate an executable file in the current directory. If you need to generate the appropriate files under $gopath/bin, go to install, or use the Go build-o path/a.exe.

3. If you have multiple files under a project folder and you just want to compile a file, you can add a filename after the go build, such as the Go Build a.go;go build command compiles all the go files in the current directory by default.

You can also specify the file name of the compiled output. For example, in section 1.2 of the Mathapp application, we can specify go build-o astaxie.exe, which is either your package name (not the main one) or the file name (main package) of the first source file.

(Note: In fact, the package name is used in the Go Language specification for the name of the "package" in the code, which can be different from the folder name.) The default generated executable file name is the folder name. )

Go build Ignores "_" or "." In the directory. Start with the go file.

If your source code requires different processing for different operating systems, you can name the files according to the different operating system suffixes. For example, there is a program that reads an array, which may have several source files 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). For example, the following compilation of Linux system only selects the Array_linux.go file, and all other system naming suffix files are ignored.

Go clean

This command is used to remove the generated files from the current source package. These files include

_obj/             Old Object directory, left by makefiles legacy _test/            old Test directory, _testmain.go     old gotest file left by makefiles, Legacy test by Makefiles.  out          old Test record, legacy build by Makefiles.  out         old Test record, left by Makefiles *. [ 568AO]        object file, Makefiles legacy dir (. exe) generated        by Go build dir.test (. exe   )-C generated mainfile (. exe)   generated by Go build mainfile.go

I generally use this command to clear the compilation file, and then GitHub to submit the source code, in the native test when these compile files are related to the system, but for the source management is not necessary.

Go FMT

Readers who have had a C + + experience will know that some people often argue that the code takes K&R style or ANSI style. In go, the code has a standard style. Because of some of the habits or other reasons we used to write code in ANSI style or other more appropriate format, this will add an unnecessary burden to people when they read someone else's code, so go enforces the code format (for example, the opening brace must be placed at the end of the line), and code that does not follow this format will not compile , in order to reduce wasted time on typesetting, the Go toolset provides a go FMT command that can help you format your written code files so that you don't need to care about the format when you write the code, you only need to execute go FMT < filename >.go After you finish writing, Your code has been modified to the standard format, but I rarely use this command, because the development tool usually has a save time automatic formatting function, which is actually called go fmt at the bottom of the function. In the next section I'll cover two tools, both of which have their own automated go FMT when saving files.

Use the Go FMT command, more often with gofmt, and require the parameter-W, otherwise the formatting results will not be written to the file. Gofmt-w src, you can format the entire project.

Go get

This command is used to dynamically obtain remote code packages, currently supported by BitBucket, GitHub, Google code, and Launchpad. This command is actually divided into two steps inside: The first step is to download the source package, 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)

So in order for go get to work properly, you have to make sure that you have the appropriate source management tools installed and add these commands to your path at the same time. In fact, go get supports the ability to customize the domain name, see go help remote for details.

Go Install

This command is actually divided internally 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 $gopath/pkg or $gopath/bin.

Go test

By executing this command, the file named *_test.go is automatically read from the source directory, and the executable file for the test is generated and run. The output information is similar

OK   Archive/tar   0. 011sFAIL archive/zip   0. 022sok   Compress 0 . 033s ...

By default, no parameters are required, and it automatically tests all the test files under your source package, but you can also take the parameters, for more information, please refer to go help Testflag

Go doc

(1.2rc1 does not have go doc instructions, leaving only godoc instructions) Many people say that go does not need any third-party documents, such as the CHM manual (in fact, I have done one, the CHM manual), because it has a very powerful document tool inside.

How do I view the documentation for the package? For example BUILTIN package, then execute Go doc builtin if it is an HTTP packet, then execute GO Doc net/http see the function inside a package, then execute Godoc fmt Printf can also view the corresponding code, execute GODOC-SRC FMT Pr intf

Execute godoc-http= on command line via command: port number such as godoc-http=:8080. Then open 127.0.0.1:8080 in the browser, you will see a golang.org local copy version, through which you can query the PKG document and other content. If you set up Gopath, under the PKG category, not only will the standard package document be listed, but also the documentation for all the items in your local gopath, which is a good choice for users who are often wall-related.

Other commands

Go also offers a number of other tools, such as the following

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 run to compile and run the Go program

Go Language Command line operation command details

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.