Golang is an open source programming language, which is characterized by efficiency, simplicity, and reliability. It is not difficult to set up a Golang environment. This article will introduce the steps to set up a Golang environment on a Linux system so that you can get started easily.
$ tar -C /usr/local -xzf go1.15.3.linux-amd64.tar.gz
The above command will decompress the compressed package and install it to the "/usr/local/go" path. The installation path here is adjustable and depends on your installation path in the Linux environment. You can also specify the decompression path through the parameter "-C" of the command "tar -C", such as:
$ tar -C /opt -xzf go1.15.3.linux-amd64.tar.gz
$ export PATH=/usr/local/go/bin:$PATH
To persist this environment variable, you can modify the "~/.bashrc" or "~/.zshrc" file. Add the following statement at the end of the file:
export PATH=$PATH:/usr/local/go/bin
Then restart your terminal window.
$ go version
If the installation is successful, you should be able to see the following output:
go version go1.15.3 linux/amd64
This means you have successfully installed Golang environment.
package main import "fmt" func main() { fmt.Println("Hello, world!") }
Save the above code to a file named "hello.go", and compile it and generate an executable binary file through the following command :
$ go build hello.go
This command will generate a binary executable file named "hello" in the same directory. Now you can run the program in the command line:
$ ./hello
If you see the following output:
Hello, world!
Congratulations, you have written and successfully run a program using Golang.
Summary
Now, you already know how to set up a Golang environment in a Linux environment. With these simple steps, you can start writing programs in this efficient, simple, and reliable programming language. Hope this article is helpful to you.
The above is the detailed content of Steps to build Golang environment on Linux system. For more information, please follow other related articles on the PHP Chinese website!