Home  >  Article  >  Backend Development  >  Guide to installing golang in Ubuntu

Guide to installing golang in Ubuntu

WBOY
WBOYOriginal
2024-01-20 10:32:061225browse

Guide to installing golang in Ubuntu

Golang installation tutorial in Ubuntu environment, specific code examples are required

Overview:
Golang (also known as Go) is an open source programming language, developed by Developed by Google. It is widely popular for its concise syntax, efficient performance and powerful tool chain. This article will introduce the detailed steps to install golang on the Ubuntu operating system and provide specific code examples.

Step 1: Install Golang

  1. Open the terminal (Ctrl Alt T) and enter the following command to download the golang installation package:

    wget https://golang.org/dl/go1.16.7.linux-amd64.tar.gz
  2. Extract the installation package:

    tar -xvf go1.16.7.linux-amd64.tar.gz
  3. Move the decompressed folder to the /usr/local directory:

    sudo mv go /usr/local
  4. Configure environment variables. Open a terminal and enter the following command to edit the ~/.profile file:

    nano ~/.profile
  5. Add the following two lines at the end of the file:

    export GOPATH=$HOME/go
    export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
  6. Save and close the file, then enter the following command to update the environment variables:

    source ~/.profile
  7. Verify that Golang is installed successfully. Enter the following command in the terminal:

    go version

    If something similar to the following is displayed, the installation is successful:

    go version go1.16.7 linux/amd64

Step 2: Write and run the sample code
Now, We will write a simple example code to verify that Golang is installed correctly.

  1. Open a text editor and create a file named hello.go;

    nano hello.go
  2. in the file Enter the following code:

    package main
    
    import "fmt"
    
    func main() {
     fmt.Println("Hello, World!")
    }
  3. Save and close the file. In the terminal, go to the directory containing the hello.go file and enter the following command to compile the code:

    go build hello.go
  4. Run the resulting executable:

    ./hello

    If everything goes well, you will see the following output in the terminal:

    Hello, World!

Summary:
Install and configure Golang in Ubuntu operating system by following the above steps , we were able to successfully write and run the sample code. Golang has become the language of choice for many developers due to its simplicity, efficiency and scalability. Hopefully this article has provided you with a clear guide to easily start developing your projects using golang.

The above is the detailed content of Guide to installing golang in Ubuntu. 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