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

Chapter 2. The Go compiler


The Go compiler is a build tool and dependency manager for the Go programming language. It offers error checking and optimization of your code.

2.1. Prerequisites

2.2. Setting up a Go workspace

To compile a Go program, you need to set up a Go workspace.

Procedure

  1. Create a workspace directory as a subdirectory of $GOPATH/src.

    A common choice is $HOME/go.

  2. Place your source files into your workspace directory.
  3. Set the location of your workspace directory as an environment variable to the $HOME/.bashrc file by running:

    $ echo 'export GOPATH=<workspace_dir>' >> $HOME/.bashrc
    $ source $HOME/.bashrc
    Copy to Clipboard Toggle word wrap

    Replace <workspace_dir> with the name of your workspace directory.

2.3. Compiling a Go program

You can compile your Go program using the Go compiler. The Go compiler creates an executable binary file as a result of compiling.

Prerequisites

Procedure

In your project directory, run:

  • On Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.17 'go build <output_file>'
    Copy to Clipboard Toggle word wrap
    • Replace <output_file< with the desired name of your output file and <go_main_package> with the name of your main package.
  • On Red Hat Enterprise Linux 8:

    $ go build <output_file>
    Copy to Clipboard Toggle word wrap
    • Replace <output_file> with the desired name of your output file and <go_main_package> with the name of your main package.
  • On Red Hat Enterprise Linux 9:

    $ go build <output_file>
    Copy to Clipboard Toggle word wrap
    • Replace <output_file> with the desired name of your output file and <go_main_package> with the name of your main package.

2.4. Running a Go program

The Go compiler creates an executable binary file as a result of compiling. Complete the following steps to execute this file and run your program.

Prerequisites

Procedure

To run your program, run in the directory containing the executable file:

$ ./<file_name>
Copy to Clipboard Toggle word wrap
  • Replace <file_name> with the name of your executable file.

2.5. Installing compiled Go projects

You can install already compiled Go projects to use their executable files and libraries in further Go projects. After installation, the executable files and libraries of the project are copied to according directories in the Go workspace. Its dependencies are installed as well.

Prerequisites

Procedure

To install a Go project, run:

  • On Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.17 'go install <go_project>'
    Copy to Clipboard Toggle word wrap
    • Replace <go_project> with the name of the Go project you want to install.
  • On Red Hat Enterprise Linux 8:

    $ go install <go_project>
    Copy to Clipboard Toggle word wrap
    • Replace <go_project> with the name of the Go project you want to install.
  • On Red Hat Enterprise Linux 9:

    $ go install <go_project>
    Copy to Clipboard Toggle word wrap
    • Replace <go_project> with the name of the Go project you want to install.

2.6. Downloading and installing Go projects

You can download and install third-party Go projects from online resources to use their executable files and libraries in further Go projects. After installation, the executable files and libraries of the project are copied to according directories in the Go workspace. Its dependencies are installed as well.

Prerequisites

Procedure

  • To download and install a Go project, run:

    • On Red Hat Enterprise Linux 7:

      $ scl enable go-toolset-1.17 'go get <third_party_go_project>'
      Copy to Clipboard Toggle word wrap
      • Replace <third_party_go_project> with the name of the project you want to download.
    • On Red Hat Enterprise Linux 8:

      $ go get <third_party_go_project>
      Copy to Clipboard Toggle word wrap
      • Replace <third_party_go_project> with the name of the project you want to download.
    • On Red Hat Enterprise Linux 9:

      $ go get <third_party_go_project>
      Copy to Clipboard Toggle word wrap
      • Replace <third_party_go_project> with the name of the project you want to download.
  • For information on possible values of third-party projects, run:

    • On Red Hat Enterprise Linux 7:

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

      $ go help importpath
      Copy to Clipboard Toggle word wrap
    • On Red Hat Enterprise Linux 9:

      $ go help importpath
      Copy to Clipboard Toggle word wrap
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

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

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

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

Red Hat 소개

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

Theme

© 2025 Red Hat