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

Chapter 2. The Go compiler


2.1. Prerequisites

2.2. Setting up a Go workspace

Modern Go projects are built using modules. You can start with a single module and then optionally group multiple modules into a workspace to work on them simultaneously.

Procedure

  1. Create a root directory for your projects, for example:

    $ mkdir ~/go-projects/
    Copy to Clipboard Toggle word wrap
  2. Change into the project directory:

    $ cd ~/go-projects/
    Copy to Clipboard Toggle word wrap
  3. Initialize a module:

    1. Create a directory for your module:

      $ mkdir <module_name>
      Copy to Clipboard Toggle word wrap
    2. Change into the module’s directory:

      $ cd <module_name>
      Copy to Clipboard Toggle word wrap
    3. Initialize the module:

      $ go mod init <module_name>
      Copy to Clipboard Toggle word wrap

      This command creates a single-module project.

    If you want to create multiple modules, repeat this step for every module.

  4. If you want to work on multiple modules at the same time, create a multi-module workspace:

    1. Change into the project directory:

      $ cd ~/go-projects/
      Copy to Clipboard Toggle word wrap
    2. Initialize a workspace to include multiple modules:

      $ go work init <module_name_1> <module_name_n> ...
      Copy to Clipboard Toggle word wrap

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

  • Compile the Go sources in the current directory:

    $ go build .
    Copy to Clipboard Toggle word wrap

2.4. Running a Go program

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

Procedure

  • Use one of the following options to execute your Go program:

    • To run an compiled program, enter:

      $ ./<file_name>
      Copy to Clipboard Toggle word wrap

      Replace <file_name> with the name of your executable file.

    • To compile the sources in the current directory and run the program in a single step, enter:

      $ go run .
      Copy to Clipboard Toggle word wrap

2.5. Installing compiled 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 according to the directories in the Go workspace. Its dependencies are installed as well.

Prerequisites

Procedure

  • Install a Go project:

    $ go install <go_project>
    Copy to Clipboard Toggle word wrap

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 according to the directories in the Go workspace. Its dependencies are installed as well.

Prerequisites

Procedure

  1. To download and install a Go project, enter:

    $ go install <third_party_go_project>
    Copy to Clipboard Toggle word wrap
  2. Optional: For information on possible values of third-party projects, enter:

    $ 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