Golang Build Command parsing

Source: Internet
Author: User

  go buildis a very common command that we can start compiling, compiling our packages and related dependencies into an executable file.

Usage:go build [-O output] [-i] [build flags] [packages]

  go buildThe use of a relatively concise, all parameters can be ignored, until only go build , this time means to use the current directory to compile, the following several commands are equivalent:

Go Buildgo build. Go Build Hello.go

The above three kinds of writing, all use the current directory compiled meaning. Because we ignored it packages , we naturally compiled it using the current directory. From here we can also extrapolate that go build essentially a path is needed so that the compiler can find out which go files need to be compiled. packagesis actually a relative path, is relative to what we define GOROOT and GOPATH These two environment variables, so with packages This parameter, go build we can know which need to compile the go file.

Go Build Flysnow.org/tools

This is the way to specify the package, which will explicitly compile our package. Of course we can also use wildcard characters

Go Build flysnow.org/tools/...

3 dots indicate that all strings are matched, so go build all packages under the Tools directory are compiled.

go buildwhen it comes to compiling, it's not a cross-platform compilation, go provides a compile-chain tool that allows us to compile executable files for other platforms on any development platform.

By default, all of our current machine-generated executables, such as your Linux 64-bit, generate executable files under Linux 64-bit, like my Mac, and you can use go env to view the compilation environment, which captures the important parts below.

~Go envgoarch="AMD64"Goexe=""Gohostarch="AMD64"Gohostos="Darwin"GOOS="Darwin"Goroot="/usr/local/go"Gotooldir="/usr/local/go/pkg/tool/darwin_amd64"

Note that there are two important environment variables GOOS and Goarch, where GOOS refers to the target operating system, and its available values are:

    1. Darwin

    2. Freebsd

    3. Linux

    4. Windows

    5. Android

    6. Dragonfly

    7. NetBSD

    8. Openbsd

    9. Plan9

    10. Solaris

There are 10 operating systems supported altogether. Goarch refers to the architecture of the target processor and is currently supported by:

    1. Arm

    2. Arm64

    3. 386

    4. Amd64

    5. Ppc64

    6. Ppc64le

    7. Mips64

    8. Mips64le

    9. s390x

With a total of 9 processor architectures, GOOS and Goarch combine to support the generation of a wide variety of executable programs, specific combinations of reference:

Https://golang.org/doc/install/source#environment

If we are going to generate executable programs for different platform architectures, just change these two environment variables, such as to build a Linux 64-bit program, the command is as follows:

Goos=linux goarch=amd64 Go Build Flysnow.org/hello

The first two assignments are changes to the environment variables, so the benefit is only valid for this run and will not change our default configuration.

These usages are almost enough for us to use, and more information about go build the user can be viewed through the following commands:

Go help build

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.