Este conteúdo não está disponível no idioma selecionado.
Chapter 2. go
go is a build tool and dependency manager for the Go programming language.
Go Toolset is distributed with go 1.10.3.
2.1. Installing go Copiar o linkLink copiado para a área de transferência!
In Go Toolset, go is provided by the go-toolset-1.10-golang package and is automatically installed with the go-toolset-1.10 package. See Section 1.4, “Installing Go Toolset”.
2.2. Writing Go 1.10.3 Programs Copiar o linkLink copiado para a área de transferência!
When creating a Go program, developers must follow the rules for Go workspace layout. The .go source files must be placed in subdirectory of $GOPATH/src.
Example 2.1. Creating a Go Program
Consider a program named hello consisting of a single source file named hello.go:
mkdir -p $GOPATH/src/hello cd $GOPATH/src/hello touch hello.go
$ mkdir -p $GOPATH/src/hello
$ cd $GOPATH/src/hello
$ touch hello.go
Edit the file hello.go in your favorite text editor to add the following text:
Additional Resources
- Workspaces — Description of the Go language workspace organization. Official documentation for the Go programming language.
2.3. Using the go Compiler Copiar o linkLink copiado para a área de transferência!
To build a Go program using the command line, change to the project directory and run the go compiler as follows:
scl enable go-toolset-1.10 'go build -o output_file go_main_package'
$ scl enable go-toolset-1.10 'go build -o output_file go_main_package'
This creates a binary file named output_file in the current working directory. If the -o option is omitted, the compiler creates a file named after the go_main_package, go_main_package.
If go_main_package is not a main package or if multiple projects or *.go files are specified, the resulting binaries are discarded. In that case, the go build command is used to verify that the supplied projects or files can be built.
Note that you can execute any command using the scl utility, causing it to be run with the Go Toolset binaries available. This allows you to run a shell session with Go Toolset go directly available:
scl enable go-toolset-1.10 'bash'
$ scl enable go-toolset-1.10 'bash'
Example 2.2. Compiling a Go Program Using the Command Line
Assuming that you have successfully created the program hello as shown in Example 2.1, “Creating a Go Program”, compile the program:
scl enable go-toolset-1.10 'go build hello.go'
$ scl enable go-toolset-1.10 'go build hello.go'
This creates a new binary file called hello in the current working directory.
2.4. Running a Go Program Copiar o linkLink copiado para a área de transferência!
When go compiles a program, it creates an executable binary file. To run this program on the command line, change to the directory with the executable file and run the program:
./file_name
$ ./file_name
Example 2.3. Running a Go Program on the Command Line
Assuming that you have successfully compiled the hello binary file as shown in Example 2.2, “Compiling a Go Program Using the Command Line”, run it by typing the following at a shell prompt:
./hello
$ ./hello
Hello.
Starting http server.
Go to localhost:8080/welcome
To terminate press CTRL+C.
2.5. Installing Go Projects Copiar o linkLink copiado para a área de transferência!
Installing a Go project means that its executable files and libraries are compiled, and copied to appropriate directories in the Go Workspace. The go tool can then use the executable files and libraries in further projects. Dependencies of the installed project are installed, too.
To install a Go project, run the go tool:
scl enable go-toolset-1.10 'go install go_project'
$ scl enable go-toolset-1.10 'go install go_project'
The install command accepts the same options as the build command.
2.6. Downloading Go Projects Copiar o linkLink copiado para a área de transferência!
To download a 3rd party Go project from an online source and install it, run the go tool:
scl enable go-toolset-1.10 'go get 3rd_party_go_project'
$ scl enable go-toolset-1.10 'go get 3rd_party_go_project'
For more details about the possible values of 3rd_party_go_project option, run the following command:
scl enable go-toolset-1.10 'go help importpath'
$ scl enable go-toolset-1.10 'go help importpath'
2.7. Additional Resources Copiar o linkLink copiado para a área de transferência!
A detailed description of the go compiler and its features is beyond the scope of this book. For more information, see the resources listed below.
Installed Documentation
The Go compiler
helpcommand provides information on its usage. To show the help index:scl enable go-toolset-1.10 'go help'
$ scl enable go-toolset-1.10 'go help'Copy to Clipboard Copied! Toggle word wrap Toggle overflow The Go compiler
doccommand shows documentation for packages. To show documentation for package package_name:scl enable go-toolset-1.10 'go doc package_name'
$ scl enable go-toolset-1.10 'go doc package_name'Copy to Clipboard Copied! Toggle word wrap Toggle overflow To learn more about the
doccommand:scl enable go-toolset-1.10 'go help doc'
$ scl enable go-toolset-1.10 'go help doc'Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Online Documentation
- Command go — Official documentation of the go compiler.
See Also
- Chapter 1, Go Toolset — An overview of Go Toolset and more information on how to install it on your system.