How to install golang on ubuntu

PHPz
Release: 2023-03-22 14:19:34
Original
3616 people have browsed it

Ubuntu is a popular Linux operating system whose user interface and open-source nature make it the operating system of choice for many programmers. Golang (also known as Go) is an open source programming language developed by Google to increase programmer productivity and simplify system-level programming. In this article, we will learn how to install Golang on Ubuntu and start using it.

1. Download Golang

First, we need to obtain the installation package from the Golang official website. Open your browser and go to the following URL: https://golang.org/dl/.

You will see a list of current versions of Golang. Make sure to select the version that corresponds to your version of Ubuntu. For example, if you are using Ubuntu 18.04, you should download go1.15.3.linux-amd64.tar.gz.

2. Unzip the file

After the download is complete, unzip the downloaded file to the location of your choice. In most cases, Golang should be in the /usr/local directory after decompression, so we use the following command here:

sudo tar -C /usr/local -xzf go1.15.3.linux-amd64.tar.gz
Copy after login

3. Configure environment variables

Now we need to configure Ubuntu’s environment variables so that the system knows where Golang is. Open a terminal and type the following command:

export PATH=$PATH:/usr/local/go/bin
Copy after login
Copy after login

In order to make the environment variable permanent, we need to add it to the user's .profile file. Open a text editor and enter the following command:

nano ~/.profile
Copy after login

Add the following code at the end of the file:

export PATH=$PATH:/usr/local/go/bin
Copy after login
Copy after login

Press Ctrl X and click Y to save the changes.

4. Test installation

After the installation is completed, we need to test whether Golang is working properly. Enter the following command in the terminal window:

go version
Copy after login

If the installation is correct, several lines of text will be displayed, including the version number of golang.

5. Install the editor

You will also need a text editor to write and run Golang code. In Ubuntu, there are many options to choose from, such as Atom or VSCode. Choose your favorite editor and install it. For example, to install VSCode, use the following command:

sudo snap install --classic code
Copy after login

6. Write and run your first Golang program

Now you are ready to run on Ubuntu Write and run Golang programs. Open an editor of your choice and create a main.go file. Paste the following code into the file:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
Copy after login

After saving and exiting, we need to compile and run the program in the terminal. Move to the directory where the main.go file is located and type the following command:

go run main.go
Copy after login

You should see "Hello, World!" output from the terminal.

Conclusion

In this tutorial, we learned how to install Golang on Ubuntu and start using it. Now you know how to set up a development environment and write your first program. Have fun programming in Golang.

The above is the detailed content of How to install golang on ubuntu. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template