이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 2. go


go is a build tool and dependency manager for the Go programming language.

Go Toolset is distributed with go 1.11.6.

2.1. Installing go

In Go Toolset on Red Hat Enterprise Linux 7, go is provided by the go-toolset-1.11-golang package and is automatically installed with the go-toolset-1.11 package. On Red Hat Enterprise Linux 8, go is provided by the go-toolset module. See Section 1.4, “Installing Go Toolset”.

2.2. Writing Go 1.11.6 Programs

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
Copy to Clipboard Toggle word wrap

Edit the file hello.go in your favorite text editor to add the following text:

package main

import (
    "fmt"
    "net/http"
)

func Welcome(w http.ResponseWriter, req *http.Request) {

    fmt.Fprintf(w, "<h1>Welcome to the Go toolset.</h1>")

}

func main() {

    fmt.Println("Hello.")
    fmt.Println("Starting http server.")
    // Register handler function
    http.HandleFunc("/welcome", Welcome)
    fmt.Println("Go to localhost:8080/welcome")
    fmt.Println("To terminate press CTRL+C.")
    // Start server
    http.ListenAndServe(":8080", nil)

}
Copy to Clipboard Toggle word wrap

Additional Resources

  • Workspaces — Description of the Go language workspace organization. Official documentation for the Go programming language.

2.3. Using the go Compiler

To build a Go program using the command line, change to the project directory and run the go compiler as follows:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.11 'go build -o output_file go_main_package'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ go build -o output_file go_main_package
    Copy to Clipboard Toggle word wrap

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 on Red Hat Enterprise Linux 7, 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.11 'bash'
Copy to Clipboard Toggle word wrap

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:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.11 'go build hello.go'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ go build hello.go
    Copy to Clipboard Toggle word wrap

This creates a new binary file called hello in the current working directory.

2.4. Running a Go Program

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
Copy to Clipboard Toggle word wrap

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.
Starting http server.
Go to localhost:8080/welcome
To terminate press CTRL+C.
Copy to Clipboard Toggle word wrap

2.5. Installing Go Projects

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:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.11 'go install go_project'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ go install go_project
    Copy to Clipboard Toggle word wrap

The install command accepts the same options as the build command.

2.6. Downloading Go Projects

To download a 3rd party Go project from an online source and install it, run the go tool:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.11 'go get 3rd_party_go_project'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ go get 3rd_party_go_project
    Copy to Clipboard Toggle word wrap

For more details about the possible values of 3rd_party_go_project option, run the following command:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.11 'go help importpath'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ go help importpath
    Copy to Clipboard Toggle word wrap

2.7. Additional Resources

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 help command provides information on its usage. To show the help index:

    • For Red Hat Enterprise Linux 7:

      $ scl enable go-toolset-1.11 'go help'
      Copy to Clipboard Toggle word wrap
    • For Red Hat Enterprise Linux 8:

      $ go help
      Copy to Clipboard Toggle word wrap
  • The Go compiler doc command shows documentation for packages. To show documentation for package package_name:

    • For Red Hat Enterprise Linux 7:

      $ scl enable go-toolset-1.11 'go doc package_name'
      Copy to Clipboard Toggle word wrap

      To learn more about the doc command:

      $ scl enable go-toolset-1.11 'go help doc'
      Copy to Clipboard Toggle word wrap
    • For Red Hat Enterprise Linux 8:

      $ go doc package_name
      Copy to Clipboard Toggle word wrap

      To learn more about the doc command:

      $ go help doc
      Copy to Clipboard Toggle word wrap

Online Documentation

  • Command go — Official documentation of the go compiler.

See Also

맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2026 Red Hat