Home >Backend Development >Golang >Golang deployment and installation guide for setting up a Go development environment

Golang deployment and installation guide for setting up a Go development environment

WBOY
WBOYOriginal
2024-02-25 23:57:20551browse

Golang deployment and installation guide for setting up a Go development environment

In order to facilitate developers to quickly set up a Go language development environment, we will introduce in detail how to deploy and install Golang on different operating systems, and how to configure the Go programming environment, with specific details. code example.

1. Installation and configuration under Windows platform

For Windows users, installing and configuring the Go language environment is very simple. Just follow the following steps:

  1. Visit the Go official website (https://golang.org/) to download the Golang installation package corresponding to the Windows system, and double-click to run the installation program.
  2. During the installation process, select the installation directory and add the installation path of the Go language to the system environment variable. You can check whether Go is successfully installed by running the following command:

    go version
  3. Configure the GOPATH (working directory) and GOROOT (Go language installation path) environment variables. Open a command line window and execute the following command:

    setx GOPATH C:path    oyourworkspace
    setx GOROOT C:Go
  4. After completing the above steps, you can create a simple Go project, such as the HelloWorld program. Create a new file hello.go and enter the following code:

    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, World!")
    }
  5. Enter the directory where hello.go is located on the command line and execute The following command compiles and runs the program:

    go run hello.go
  6. If you see the output Hello, World!, it means that the Go language environment has been successfully installed and configured.

2. Installation and configuration under macOS platform

For macOS users, installing and configuring the Go language development environment is equally simple and fast. The following are the specific steps:

  1. Execute the following command in the terminal to install Golang using Homebrew:

    brew install golang
  2. After the installation is complete, execute the following command Check the Go language version:

    go version
  3. Configure the GOPATH and GOROOT environment variables. Execute the following command in the terminal:

    export GOPATH=/path/to/your/workspace
    export GOROOT=/usr/local/opt/go/libexec
  4. Create a simple Go project and write the HelloWorld program. Also create a new hello.go file and enter the following code:

    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, World!")
    }
  5. Enter the directory where hello.go is located in the terminal and run the program:

    go run hello.go
  6. If you see the output Hello, World!, it means that the Go language environment is successfully configured under macOS.

3. Installation and configuration under the Linux platform

For Linux users, the installation and configuration of Golang is also very simple and clear. The following are detailed steps:

  1. Execute the following command in the terminal and use apt or yum package manager to install Golang:

    sudo apt-get update
    sudo apt-get install golang
  2. After the installation is completed , execute the following command to check the Go language version:

    go version
  3. Similarly, configure the GOPATH and GOROOT environment variables. Execute the following command in the terminal:

    export GOPATH=/path/to/your/workspace
    export GOROOT=/usr/lib/go
  4. Create a simple Go project and write the HelloWorld program. Also create a new hello.go file and enter the following code:

    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, World!")
    }
  5. Enter the directory where hello.go is located in the terminal and run the program:

    go run hello.go
  6. If you see the output Hello, World!, it means that the Go language environment is successfully configured under Linux.

Through the above steps and sample code, we have introduced in detail the steps to deploy and configure the Golang development environment on Windows, macOS and Linux platforms. I hope it can help developers who are learning and using Go language to quickly get started and enjoy the charm of Go language.

The above is the detailed content of Golang deployment and installation guide for setting up a Go development environment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn