程序Linux上运行Go语言程序的指南(linux运行go)

Nowadays, Go language has become one of the most popular programming languages for developers, and this guide is about running Go program on Linux platform.

1. Download and install Go

Before running Go code on any platform, the first step is to install the language itself. To do this, the most popular way is to download and install the Go binaries:

$ wget https://dl.google.com/go/go1.14.linux-amd64.tar.gz # download Go

$ tar -xzf go1.14.linux-amd64.tar.gz # extract the files

$ sudo mv go /usr/local # move it to the proper location

After that, the installation should be completed.

2. Set your environment

Since Go compiles code in your environment, we need to set some environment variables to make sure the compiled code can find proper Go functions. You can accomplish this task with two commands:

$ export GOROOT=/usr/local/go # setup GOROOT for go commands

$ export PATH=$PATH:/usr/local/go/bin # append the go binary path to PATH

These two commands will link our current environment to the Go installation so that it can use the Go binaries.

3. Verify the setup

To further confirm our setup was successful, we can run some test code to verify the Go environment:

package main

import “fmt”

func main() {

fmt.Printf(“Hello, Go on Linux!\n”)

}

If the output shows “Hello, Go on Linux!”, then it shows that the setup is completed.

4. Write your own Go code

Now, with the environment ready, developers can begin to code with Go. Once the code is written, it can be build and ran:

$ go build myprogram.go # build the program

$ sudo ./myprogram # run the program

By following these steps, developers should be able to successfully install and run Go on Linux.


数据运维技术 » 程序Linux上运行Go语言程序的指南(linux运行go)