Introduction to common commands for Go languages

Source: Internet
Author: User

Go Build

The Go Build 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, no files will be generated when you execute the Go Build command.

    2. In the case of the main package, an executable file is generated in the current directory when only the go Build command is executed. 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.

    3. If you have more than one file in a folder and you just want to compile one of them, you can add a file name after the go build, such as the Go Build a.go;go build command, which compiles all the go files in the current directory by default.

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

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

    6. 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

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
Go FMT

The Go FMT command is primarily used to help you format the code files that you have written.

For example, we wrote a poorly formatted Test.go file, and we just needed to use the FMT Go test.go command to have go help us format our code files. However, we rarely use this command because our development tools generally have a save-as-you-go format feature, which is essentially a call to the went FMT command.

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

The go get command is primarily used to dynamically retrieve 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:

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.

The go get command is essentially understood to be: first, through the Source Tool clone code to the SRC directory, and then execute go install.

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.

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

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

Go doc

The Go Doc command is actually a powerful documentation tool.

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, or you can view the corresponding code, 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: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 items in your local gopath, which is a good choice for users who are often restricted to access.

Other commands

The Go language also provides other useful tools, such as the following tools

go fix 用来修复以前老版本的代码到新版本,例如go1之前老版本的代码转化到go1go version 查看go当前的版本go env 查看当前go的环境变量go list 列出当前全部安装的packagego run 编译并运行Go程序

Introduction to common commands for Go languages

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.