Golang (Go)
Install Go
- Operating system Ubuntu
-
Download package
- Find latest version on official download page
curl -OL https://golang.org/dl/go1.22.4.linux-amd64.tar.gz
-
Extract application
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.22.4.linux-amd64.tar.gz
-
Set new path
- Open file
~/.profile
and add the following path to the end of your file
export PATH=$PATH:/usr/local/go/bin
- Open file
-
Reload profile with new path
source ~/.profile
-
Verify
go
versiongo verion
Output:
go version go1.22.4 linux/amd64
Go code
After you installed Go programming language, you can try program your first program in Go, Hello, World!
.
-
Prepare directory for your project
mkdir go-example
cd go-exmaple -
Initial your project
$ go mod init example/hello
go: creating new go.mod: module example/hello -
Create new file with name
hello.go
via your code editor and insert the following codepackage main
import "fmt"
func main() {
fmt.Println("Hello, World!")
} -
Execute your code
$ go run .
Hello, World!
External packages
Sometimes you will need to implement external functions in your code. You can search https://pkg.go.dev/ for third-party packages with the right features for your project
-
Import package to your Go code
package main
import "fmt"
import "rsc.io/quote"
func main() {
fmt.Println(quote.Go())
} -
Install requirements
$ go mod tidy
go: finding module for package rsc.io/quote
go: downloading rsc.io/quote v1.5.2 -
Execute your code
$ go run .
Don't communicate by sharing memory, share memory by communicating.
Project structure
Very good examples how to structure a project in Go:
Source: