Guide to installing golang in Ubuntu

WBOY
Release: 2024-01-20 10:32:06
Original
1088 people have browsed it

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
    Copy after login
  2. Extract the installation package:

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

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

    nano ~/.profile
    Copy after login
  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
    Copy after login
  6. Save and close the file, then enter the following command to update the environment variables:

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

    go version
    Copy after login

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

    go version go1.16.7 linux/amd64
    Copy after login

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
    Copy after login
  2. in the file Enter the following code:

    package main
    
    import "fmt"
    
    func main() {
     fmt.Println("Hello, World!")
    }
    Copy after login
  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
    Copy after login
  4. Run the resulting executable:

    ./hello
    Copy after login

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

    Hello, World!
    Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!