How to install Go on Ubuntu 22.04

To Install Go On Ubuntu 22.04

Introduction:

Go was originally built for programs related to networking and infrastructure. It was intended to replace popular high-performance server-side languages like Java and C++. Today, Go is used for a variety of applications like cloud and server-side applications, DevOps, command line tools, and much more.

Procedure :

Step – 1: Check the OS version by using the below command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.3 LTS
Release:	22.04
Codename:	jammy

Step – 2 : Before installation, need to update the repository by using the below command

root@linuxhelp:~# apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:3 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu jammy-backports InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
255 packages can be upgraded. Run 'apt list --upgradable' to see them.

Step – 3 : Download Go by using wget command

root@linuxhelp:~# wget https://go.dev/dl/go1.21.4.linux-amd64.tar.gz -O go.tar.gz
--2024-03-21 13:30:54--  https://go.dev/dl/go1.21.4.linux-amd64.tar.gz
Resolving go.dev (go.dev)... 216.239.32.21, 216.239.38.21, 216.239.36.21, ...
Connecting to go.dev (go.dev)|216.239.32.21|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://dl.google.com/go/go1.21.4.linux-amd64.tar.gz [following]
--2024-03-21 13:30:54--  https://dl.google.com/go/go1.21.4.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 172.217.160.142, 2404:6800:4007:81a::200e
Connecting to dl.google.com (dl.google.com)|172.217.160.142|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 66615271 (64M) [application/x-gzip]
Saving to: ‘go.tar.gz’

go.tar.gz                         100%[===========================================================>]  63.53M  10.3MB/s    in 6.2s    

2024-03-21 13:31:01 (10.2 MB/s) - ‘go.tar.gz’ saved [66615271/66615271]

Step – 4 : Extract the downloaded file in the /usr/local location by using the below command

root@linuxhelp:~# tar -xzvf go.tar.gz -C /usr/local
go/test/typeparam/typeswitch7.go
go/test/typeparam/typeswitch7.out
go/test/typeparam/valimp.dir/
go/test/typeparam/valimp.dir/a.go
go/test/typeparam/valimp.dir/main.go
go/test/typeparam/valimp.go
go/test/typeparam/value.go
go/test/typeswitch.go
go/test/typeswitch1.go
go/test/typeswitch2.go
go/test/typeswitch2b.go
go/test/typeswitch3.go
go/test/uintptrescapes.dir/
go/test/uintptrescapes.dir/a.go
go/test/uintptrescapes.dir/main.go
go/test/uintptrescapes.go
go/test/uintptrescapes2.go
go/test/uintptrescapes3.go

Step – 5 : Set the path of Go executables in the ~/.profile, by using the below command

root@linuxhelp:~# echo export PATH=$HOME/go/bin:/usr/local/go/bin:$PATH >> ~/.profile

Step – 6 : Then, save the changes by sourcing the file by using the below command

root@linuxhelp:~# source ~/.profile

Step – 7 : Check the version of Go by using the below command

root@linuxhelp:~# go version
go version go1.21.4 linux/amd64

Step – 8 : create a simple Go program by using vim editor

root@linuxhelp:~# vim demo.go
copy the following code in the file
package main
import "fmt"
func main() {
    fmt.Println("Hello, World!")
}

Step – 9 : Run the program by using the below command

root@linuxhelp:~# go run demo.go
Hello, World!

Conclusion :

We have reached the end of this article. In this guide, we have walked you through the steps required to installation of Go on Ubuntu 22.04. Your feedback is much welcome.

FAQ
Q
What are function closures?
A
Function closures is a function value that references variables from outside its body. The function may access and assign values to the referenced variables.
Q
What form of type conversion does Go support?
A
Convert an integer to a float. Go supports explicit type conversion to satisfy its strict typing requirements.
Q
What data types does Golang use?
A
Golang uses the following types:
1. Method
2. Boolean
3. Numeric
4. String
5. Array
6. Slice
7. Struct
8. Pointer
9. Function
10. Interface
11. Map
12. Channel
Q
What is Golang mainly used for?
A
Go is used for a variety of applications like cloud and server-side applications, DevOps, and command line tools.
Q
What is the use of the Golang?
A
To develop web applications, cloud and networking services, and other types of software.