"Golang" study note 3--Go common commands

Source: Internet
Author: User

Go command

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

These commands in this diagram above are very useful for the code we normally write, and then let's look at some common commands.

Go Build

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

  • If it's a normal package, like the MyMath package we wrote in section 1.2, when you do go build , it doesn't produce any files. If you need to generate the appropriate files under the ' $GOPATH/pkg, go install** will be executed.
  • If it is a main package, it will generate an executable file in the current directory when you execute the go build . If you need to generate the appropriate file under ' $GOPATH/bin, go to install, or use the Go build-o path/a.exe**.
  • If you have multiple files under a project folder and you just want to compile a file, you can add the file name after go build , such as go build a.go;go build The 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 of the first source file (main package )。
    (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.

Introduction to Parameters

  • - o Specifies the file name of the output, which can be carried on the path, e.g. go build-o a/b/c
  • -I install the appropriate package, compile +go install
  • -A update is all the latest packages, but not for standard packages
  • - n Prints the compiled command that needs to be executed, but does not execute, so that it is easy to know how the underlying is running
  • - P n Specifies the number of compilations that can be run concurrently, by default the number of CPUs
  • -race automatically detects data contention when compiling, currently only supports 64-bit machines
  • - v print out the package name we are compiling
  • -work Print out the temporary folder name at compile time, and do not delete if it already exists
  • The command that the- x prints out executes, in fact, is similar to the results of- n , except that this will execute
  • -ccflags ' arg list ' passed parameters to 5c, 6c, 8c call
  • -compiler Name Specifies the appropriate compiler, GCCGO or GC
  • -gccgoflags ' arg list ' pass parameter to Gccgo compile connection call
  • -gcflags ' arg list ' passed parameters to 5g, 6g, 8g call
  • -installsuffix suffix in order to be different from the default installation package, using this prefix to reinstall those dependent packages,-race when the default is -installsuffix race , you can use the '-n** command to verify
  • -ldflags ' flag list ' passes parameters to 5l, 6l, 8l calls
  • -tags ' tag list ' sets the tag that can be adapted at compile time, the detailed tag limit reference inside the build Constraints

Go clean

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

_obj/            旧的object目录,由Makefiles遗留_test/           旧的test目录,由Makefiles遗留_testmain.go     旧的gotest文件,由Makefiles遗留test.out         旧的test记录,由Makefiles遗留build.out        旧的test记录,由Makefiles遗留*.[568ao]        object文件,由Makefiles遗留DIR(.exe)        由go build产生DIR.test(.exe)   由go test -c产生MAINFILE(.exe)   由go build MAINFILE.go产生*.so             由 SWIG 产生

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 clean -i -ncd /Users/astaxie/develop/gopath/src/mathapprm -f mathapp mathapp.exe mathapp.test mathapp.test.exe app app.exerm -f /Users/astaxie/develop/gopath/bin/mathapp

Parameter introduction

    • -I clears associated installed packages and executable files, that is, files installed through go install
    • - n prints out the cleanup commands that need to be executed, but does not execute, so it's easy to know how the bottom layer works
    • - R Loop clears the package introduced in import
    • - x print out the execution of the detailed command, is actually a- n print version of the execution

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 formatting when you write your code, you just 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 fmtat 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.

With the Go FMT command, the gofmt is actually called, and the parameter-W is required, otherwise the formatted result is not written to the file. Gofmt-w-l SRC, you can format the entire project.

So go FMT is a wrapper command on the upper layer of gofmt, we want more personalized formatting can refer to Gofmt

Introduction to GOFMT Parameters

    • - l display files that need to be formatted
    • - W writes the rewritten content directly to the file instead of printing to standard output as a result.
    • - R Add a rewrite rule like "A[b:len (a)], A[b:]", so that we do bulk substitution
    • - s simplifies the code in the file
    • - D show diff before and after formatting instead of writing to file, default is False
    • - e Prints all syntax errors to standard output. If you do not use this tag, only the first 10 errors that are not in the same row will be printed.
    • -cpuprofile supports debug mode, writes the corresponding cpufile to the specified file

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 remotefor details.

Parameter description:

    • - D only download does not install
    • - F is only available when you include -u**参数的时候才有效,不让 -u** to verify that each of the import has been acquired, which is particularly useful for local fork packages
    • -fix Run fix before you get the source code, and then do something else.
    • - t also downloads the packages needed to run the test
    • - u enforces the use of the network to update the package and its dependent packages
    • - v Show executed commands

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.

Parameters support the compilation parameters of go build . All you have to do is remember one parameter,- v , and you can view the underlying execution information from anywhere.

Go test

Executing this command automatically reads the file named ' *_test.go** ' under the source directory and generates and runs the executable file for the test. The output information is similar

ok   archive/tar   0.011sFAIL archive/zip   0.022sok   compress/gzip 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

Here I introduce a few of our commonly used parameters:

    • -bench regexp executes the corresponding benchmarks, such as -bench=.
    • -cover Open test coverage
    • -run regexp only run regexp matching functions, such as -run=array , then execute functions containing the beginning of the Array
    • - v Show detailed commands for the test

Go tool

Go tool Download a lot of commands below, here we only introduce two, fix and vet

    • go tool fix. Used to fix previous old versions of code into new versions, such as Go1 before the old version of the code was converted to GO1, such as API changes
    • Go Tool vet Directory|files is used to analyze whether the code in the current directory is the correct code, such as calling FMT. The parameters in printf are incorrect, such as the useless code that appears after you return to the function in advance.

Go generate

This command was designed from Go1.4 to automate the generation of certain types of code before compiling. go generate and go build are completely different commands, by analyzing special comments in the source code, and then executing the corresponding commands. These commands are very clear, there is no dependency on the inside. And everyone in the use of this before the heart must have a concept, this go generate is for you, not for the use of your bag of people, is convenient for you to generate some code.

Here we are going to give a simple example, for example, we often use YACC to generate code, so we used this kind of command:

go tool yacc -o gopher.go -p parser gopher.y

-o Specifies the file name of the output,-p specifies the name of the package, which is a separate command, if we want go generate to trigger this command, then in the course of the directory can be any one xxx.go Add a line to the following comment anywhere in the file:

//go:generate go tool yacc -o gopher.go -p parser gopher.y

Here we notice, '//go:generate** is not any space, this is actually a fixed format, in the scanning source file is based on this to judge.

So we can build, compile, test with the following commands. If the gopher.y file has been modified, then it is good to re-execute the go generate to regenerate the file.

$ go generate$ go build$ go test

Godoc

The Go Doc command was also supported before the Go1.2 version, but it was then moved to the Godoc command to install go get golang.org/x/tools/cmd/godoc

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 godoc builtin
If it is an HTTP packet, then execute godoc net/http
View the functions inside a package, then execute godoc fmt Printf
You can also view the corresponding code to execute godoc-src fmt Printf

Execute godoc-http= on command line via command: port number such as godoc-http=:8080. Then open 127.0.0.1:8080in 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 version 查看go当前的版本go env 查看当前go的环境变量go list 列出当前全部安装的packagego run 编译并运行Go程序

The tools above have many parameters that are not covered, and users can use the go Help command for more detailed assistance.

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.