Go language command line operation command details _golang_script home

Go language command line operation commands

Updated: October 28, 2014 11:14:37 Submitted by: junjie
This article mainly introduces the Go language command line operation command in detail, this article focuses on go build, go clean, go fmt, go get and other commands, need friends can refer to the next

Go command

Go comes with a complete set of command manipulation tools, which you can view by executing go from the command line:

Figure 1.3 Go command shows the details

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

go build

This command is mainly used to test compilation. During the compilation of a package, the packages associated with it are compiled at the same time, if necessary.

1. If it is a normal package, like the mymath package we wrote in Section 1.2, it does not produce any files when you execute go build. If you need to generate the appropriate file under $GOPATH/pkg, you will have to execute go install.

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

3. If there are multiple files in a project folder, and you only want to compile a file, you can add the file name after go build, for example, go build a.geo; By default, the go build command compiles all go files in the current directory.

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

Note: In fact, the package name in the Go specification refers to the name used after "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 directories that start with "_" or ". The start of the go file.

If your source code needs different processing for different operating systems, then you can name the files according to different operating system suffixes. For example, a program that reads an array may have the following source files for different operating systems:

Copy codeThe code is as follows:

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

go build selectively builds files ending in the system name (Linux, Darwin, Windows, Freebsd). For example, only the array_linux.go file is selected for the following compilation in Linux, and all other system naming suffix files are ignored.

go clean

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

Copy codeThe code is as follows:

_obj/ old object directory, left over by Makefiles
_test/ Old test directory, left over by Makefiles
_testmain.go The old gotest file, left behind by Makefiles
Test. out Indicates the old test record, which is left by the Makefiles
build.out Old test records, left over by Makefiles
*.[568ao] object file, left by Makefiles

DIR(.exe) generated by go build DIR.test(.exe) generated by go test -c MAINFILE(.exe) generated by go build mainfile.go


I usually use this command to clear the compilation file, and then github submit the source code, in the local test when these compilation files are relevant to the system, but not necessary for source management.

go fmt

Readers with C/C++ experience will know that some people often argue about whether the code should be K&R style or ANSI style. In go, the code has a standard style. Because of some custom or other reason we often write code in ANSI style or other more suitable format, which will put unnecessary burden on people to read other people's code, so go enforces the code format (for example, the open bracket must be placed at the end of the line), and code that does not conform to this format will not compile. In order to reduce the waste of time in typesetting, go tool set provides a go fmt command which can help you format your written code file, so that you do not need to care about the format when writing code, you only need to execute go fmt < file name >.go after writing, your code will be modified into a standard format. But I usually rarely use this command, because the development tools generally have a save time automatic formatting function, this function is actually at the bottom is to call go fmt. In the next section, I'll cover two tools that come with automatic go fmt functionality when saving files.

Copy codeThe code is as follows:

Use the gofmt command, more often gofmt, and need the parameter -w, otherwise the formatting result will not be written to the file. gofmt -w src to format the entire project.

go get

This command is used to dynamically retrieve remote Code packages, currently supported by BitBucket, GitHub, Google Code, and Launchpad. Internally, this command is actually divided into two steps: the first step is to download the source package, and the second step is to execute go install. The go tool that downloads the source package will automatically call different source tools according to different domain names, and the corresponding relationship is as follows:

Copy codeThe code 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, you need to make sure that you have the right source management tools installed and that you add these commands to your PATH as well. In fact, go get supports the function of custom domain name, see go help remote for details.

go install

Internally, this command is actually divided into two steps: the first step generates the result file (executable file or.a package), and the second step moves the compiled result to $GOPATH/pkg or $GOPATH/bin.

go test

Run this command to automatically read the source directory named *_test.go file, generate and run the test executable file. The output information is similar

Copy codeThe code is as follows:

ok   archive/tar   0.011s
FAIL archive/zip   0.022s
ok   compress/gzip 0.033s
...

By default, it does not need any parameters, it will automatically test all the test files under your source package, of course, you can also bring parameters, details please refer to go help testflag

go doc

(After 1.2rc1 there is no godoc instruction, leaving only godoc instructions) Many people say that go does not need any third-party documentation, such as the chm manual (actually I have made one, the chm manual), because it has a very powerful documentation tool inside.

How do I view the documentation for the corresponding package? For example, if builtin is an http package, then godoc builtin is executed. If it is an http package, then godoc net/ HTTP is executed to view the functions in one of the packages. If godoc fmt Printf is executed, you can also view the corresponding code. Run godoc -src fmt Printf

Run the godoc-http =: port number command on the CLI, for example, godoc-http =:8080. Then open 127.0.0.1:8080 in your browser and you will see a local copy of golang.org from which you can query pkg documents and other content. If you set up GOPATH, under the pkg category, not only the documentation of the standard package will be listed, but also the relevant documentation of all the projects in your local GOPATH, which is a good choice for users who are often walled.

Other commands

go also provides many other tools, such as the ones below

Copy codeThe code is as follows:

go fix is used to fix the old version of the code to the new version, for example, the old version of the code before go1 is converted to go1
go version Displays the current version of go
go env Displays the current go environment variable
go list lists all currently installed packages
go run compiles and runs the Go program

There are many parameters that are not described in the preceding tools. You can run the go help command to obtain more detailed help information.

Related article

  • 一文了解Go语言io.Copy函数

    Understand the Go language io.Copy function

    This article mainly introduces the Go language io.Copy function use examples detailed explanation, need friends can use for reference, hope to be helpful, I wish you a lot of progress, early promotion and pay rise
    2023-07-07
  • 一文带你深入了解Golang中的自旋锁

    Here's an in-depth look at spin locks in Golang

    Spin lock is a kind of busy waiting lock, when a thread tries to obtain a lock that has been held by another thread, the thread will continue to check the state of the lock (that is, "spin"), until the lock is released after the ownership, let's take a closer look at the specific operation of spin lock
    2024-01-01
  • golang并发下载多个文件的方法

    golang method for downloading multiple files concurrently

    Today, Xiaobian will share a golang and download multiple files for you, which has a good reference value, and I hope to help you. Let's take a look
    2019-07-07
  • 详解在Go语言单元测试中如何解决Redis存储依赖问题

    How to solve the Redis storage dependency problem in Go unit testing

    In writing unit tests, in addition to MySQL this external storage dependency,Redis should be another of the most common external storage dependency, this article will explain how to solve Redis external dependency, the article through the code example introduced very detailed, need friends can refer to
    2023-08-08
  • 深入理解Go语言中的结构体

    In-depth understanding of structs in the Go language

    This article mainly introduces the in-depth understanding of Go language structure, including the definition of structure, the declaration of structure variables, the use of structure, structure association function, new, combination, etc., the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, need friends to learn together with the small series
    2023-11-11
  • 文字解说Golang Goroutine和线程的区别

    Text explains the difference between Golang Goroutine and thread

    goroutine is a lightweight thread implementation in the Go language, managed by the Go runtime, using each go keyword will open a new coroutine, today through this article to introduce the difference between Golang Goroutine and threads. Interested friends take a look
    2022-03-03
  • Golang中gin框架绑定解析json数据的两种方法

    The gin framework in Golang binds two methods for parsing json data

    This article introduces Golang's gin framework to receive json data and analyze the two methods, the article through the code examples introduced very detailed, to everyone's study or work has a certain help, need friends can refer to
    2023-12-12
  • golang 如何删除二进制文件中的源码路径信息

    golang how to remove source path information in binary files

    This article mainly introduces golang how to delete the source code path information in the binary file, has a good reference value, I hope to help you. Let's take a look
    2021-04-04
  • Go 代码规范错误处理示例经验总结

    Go code specification error handling example experience summary

    This article mainly introduces the Go code specification error handling example practical experience summary, friends in need can use for reference, hope to be helpful, I wish you a lot of progress, early promotion and pay rise
    2022-08-08
  • Golang标准库unsafe源码解读

    Golang standard library unsafe source code interpretation

    This article mainly introduces the Golang standard library unsafe source code interpretation, friends in need can draw on the reference, hope to be helpful, I wish you a lot of progress, early promotion and pay rise
    2022-08-08

Latest comments