custom background image

Hoe installeer je Go op Ubuntu 22.04?


Installeer en configureer Go op Ubuntu 22.04

Doel

Go is een van de beroemdste talen ter wereld. Doordat het de afgelopen jaren veel is toegepast, kun je in development en in Ops niet om de taal heen. Voor meer informatie en de mogelijkheden van de taal Go verwijzen we naar de officiële documentatie.

In deze tutorial leert u hoe u Go installeert op een Ubuntu 22.04 Linux-distributie.

 

Vereisten

In deze zelfstudie wordt ervan uitgegaan dat u een Ubuntu 22.04 heeft, die bijvoorbeeld in een OVHcloud Compute-instance draait, en dat u enkele basiskennis heeft van hoe u dit moet doen. Als u geen actieve Ubuntu 22.04 heeft, volg dan de handleiding om een OVHcloud Compute Instance te gebruiken.

 

Instructies

In deze tutorial zult u eerst Go installeren. Daarna zult u het gebruiken en uiteindelijk zult u leren hoe u tussen verschillende geïnstalleerde versies kunt schakelen.

Bij het schrijven van deze tutorial is de laatste LTS-versie van Go 1.18.x.

 

Go installeren

De eenvoudigste manier om Go op Ubuntu te installeren is om het apt-get commando te gebruiken:

sudo apt-get update && sudo apt-get -y install golang-go

De output zou als volgt moeten zijn:

$ sudo apt-get update && sudo apt-get -y install golang-go
Hit:1 http://nova.clouds.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:3 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:4 http://security.ubuntu.com/ubuntu jammy-security InRelease
Ign:5 https://pkg.jenkins.io/debian-stable binary/ InRelease
Hit:6 https://pkg.jenkins.io/debian-stable binary/ Release
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  bzip2 cpp cpp-11 g++ g++-11 gcc gcc-11 gcc-11-base golang-1.18-go golang-1.18-src golang-src libasan6 libatomic1 libc-dev-bin libc-devtools libc6-dev libcc1-0
  libcrypt-dev libdpkg-perl libfile-fcntllock-perl libgcc-11-dev libgd3 libgomp1 libisl23 libitm1 liblsan0 libmpc3 libnsl-dev libquadmath0 libstdc++-11-dev libtirpc-dev
  libtsan0 libubsan1 linux-libc-dev manpages-dev pkg-config rpcsvc-proto
Suggested packages:
  bzip2-doc cpp-doc gcc-11-locales g++-multilib g++-11-multilib gcc-11-doc gcc-multilib autoconf automake libtool flex bison gdb gcc-doc gcc-11-multilib bzr | brz
  mercurial subversion glibc-doc debian-keyring bzr libgd-tools libstdc++-11-doc dpkg-dev
The following NEW packages will be installed:
  bzip2 cpp cpp-11 g++ g++-11 gcc gcc-11 gcc-11-base golang-1.18-go golang-1.18-src golang-go golang-src libasan6 libatomic1 libc-dev-bin libc-devtools libc6-dev libcc1-0
  libcrypt-dev libdpkg-perl libfile-fcntllock-perl libgcc-11-dev libgd3 libgomp1 libisl23 libitm1 liblsan0 libmpc3 libnsl-dev libquadmath0 libstdc++-11-dev libtirpc-dev
  libtsan0 libubsan1 linux-libc-dev manpages-dev pkg-config rpcsvc-proto
0 upgraded, 38 newly installed, 0 to remove and 14 not upgraded.
Need to get 143 MB of archives.
After this operation, 634 MB of additional disk space will be used.
...
Scanning processes...                                                                                                                                                       
Scanning candidates...                                                                                                                                                      
Scanning linux images...                                                                                                                                                    

Restarting services...
Service restarts being deferred:
 systemctl restart systemd-logind.service

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

Daarna kunt u uw nieuwe installatie testen:

go version

De output zou als volgt moeten zijn:

$ go version
go version go1.18.1 linux/amd64

Bij Go mag u meerdere geïnstalleerde versies gebruiken. Om bijvoorbeeld versie 1.17 te installeren:

go install golang.org/dl/go1.17@latest

De output zou als volgt moeten zijn:

$ go install golang.org/dl/go1.17@latest
go: downloading golang.org/dl v0.0.0-20220510203206-88ea6714b1d9

Het go-commando downloadt het binaire bestand go1.17 in de map ~/go/bin.

Vervolgens kunt u dit binaire bestand gebruiken om versie 1.17 te installeren:

~/go/bin/go1.17 download

De output zou als volgt moeten zijn:


$ ~/go/bin/go1.17 download
Downloaded   0.0% (    16384 / 134787877 bytes) ...
Downloaded  37.7% ( 50822784 / 134787877 bytes) ...
Downloaded  82.8% (111606960 / 134787877 bytes) ...
Downloaded 100.0% (134787877 / 134787877 bytes)
Unpacking /home/ubuntu/sdk/go1.17/go1.17.linux-amd64.tar.gz ...
Success. You may now run 'go1.17'

Uw nieuwe installatie van Go bevindt zich in de map /home/ubuntu/sdk/go1.17.

U kunt uw PATH omgevingsvariabele updaten, als u deze versie wilt gebruiken:

export PATH=/home/ubuntu/sdk/go1.17/bin:$PATH go version

De output zou als volgt moeten zijn:


$ export PATH=/home/ubuntu/sdk/go1.17/bin:$PATH
$ go version
go version go1.17 linux/amd64

 

Test Go-installatie

Om uw Go installatie te testen, kunt u een applicatie als Hello World schrijven. Creëer een helloworld.go-bestand en plak daarin de volgende code:

package main import "fmt" func main() { fmt.Println("👋 Hello World.") }

Sla het bestand op en voer het uit:

go run helloworld.go

De output zou als volgt moeten zijn:


$ go run helloworld.go 
👋 Hello World.

Dat is alles. U heeft met succes Go op Ubuntu 22.04 geïnstalleerd en geconfigureerd.

 

Verdere stappen

Bekijk de producten voor Public Cloud-instances op OVHcloud.