Location>code7788 >text

Linux/Go environment setup, HelloWorld running

Popularity:992 ℃/2024-08-16 10:56:32

package
main
import "fmt" func main() { ("Hello,World!!!\n") }

Above is the source code of the classic HelloWorld program in Go language.

Linux/GO environment setup

When the author first learned Linux/Go, he compiled and ran his first Go program, HelloWorld, using Go's command-line tools:

$ go build 

Here go is an executable program that needs to be configured with environment variables to take effect. The next step is to document the process of downloading and installing Linux/Go.

Golang official website download address:/dl/

GO Language Chinese website address:/dl

downloading

The author downloaded version 1.22.5 of Go, download link: /dl/golang/go1.22., using Linux can be direct:

$ wget https:///dl/golang/go1.22.

unzip and install

Download this zip file and extract it using the following command:

$ tar -C /usr/local -zxvf go1.22.

The -C /usr/local option here means to install GO under /usr/local/. After the installation is complete, you will find a new directory "go" under /usr/local/:

At this point, Go has been installed, the above described compilation of Go programs with the executable program "go" in the go/bin/ directory.

Configuring Environment Variables

Once you've done the above, you can run go explicitly with an absolute path:

$ ./usr/local/go/bin/go build 

This command explicitly calls the executable program "go" in the /usr/local/go/bin directory to compile the source code for the go language. Next, you configure environment variables to allow the system to find the path to go on its own.

Edit the file /etc/profile.

$ vim /etc/profile
# Add the following two lines to the file:
$ export GOROOT=/usr/local/go
$ export PATH=$PATH:$GOROOT/bin

Source it:

source /etc/profile

This is the environment variable just fine. Using go version to view it successfully outputs the version of go.

Now you can run the go program with a clean slate. Create a file in a random folder, name it, compile it, and run it: