Go Language Basics
Go
Go is an open source programming language supported by Google. As per the official website it is easy to learn and get started with. It is syntactically similar to C
language but with additional features.
This week, I wanted to get started with this mainly because of Kubernetes. Kubernetes source code is in the Go
language.
So let’s get started.
Download and Install
I followed the instructions given here on the official web site to download and install Go on macOS.
pradeep@LearnGo Go % go version
go version go1.18.1 darwin/amd64
pradeep@LearnGo Go %
Code Go
Enable dependency tracking for your code.
When your code imports packages contained in other modules, you manage those dependencies through your code’s own module. That module is defined by a go.mod file that tracks the modules that provide those packages. That go.mod file stays with your code, including in your source code repository.
To enable dependency tracking for your code by creating a go.mod file, run the go mod init
command, giving it the name of the module your code will be in. The name is the module’s module path.
pradeep@LearnGo Go % go mod init example/hello
go: creating new go.mod: module example/hello
go: to add module requirements and sums:
go mod tidy
pradeep@LearnGo Go % ls
go.mod pkg
pradeep@LearnGo Go % mkdir example
pradeep@LearnGo Go % mv go.mod example
pradeep@LearnGo Go % cd example
Let us write our first Go code, using the sample given for printing Hello World!
.
pradeep@LearnGo example % ls
go.mod hello.go
pradeep@LearnGo example % cat hello.go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
pradeep@LearnGo example %
In this code, this is what we have done.
- Declare a
main
package (a package is a way to group functions, and it’s made up of all the files in the same directory). - Import the popular
fmt
package, which contains functions for formatting text, including printing to the console. This package is one of the standard library packages you got when you installed Go. - Implement a
main
function to print a message to the console. Amain
function executes by default when you run themain
package.
Let us run this program, with the go run
command.
pradeep@LearnGo example % go run hello.go
Hello, World!
pradeep@LearnGo example %
We can use the pkg.go.dev
site to find published modules whose packages have functions that we can use in our own code. Packages are published in modules – like rsc.io/quote
.
Let us use this external package in our program,
pradeep@LearnGo example % cat quote.go
package main
import "fmt"
import "rsc.io/quote"
func main() {
fmt.Println(quote.Go())
}
pradeep@LearnGo example %
Run the go mod tidy
command.
pradeep@LearnGo example % go mod tidy
go: finding module for package rsc.io/quote
go: downloading rsc.io/quote v1.5.2
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
pradeep@LearnGo example %
pradeep@LearnGo example % go run quote.go
Don't communicate by sharing memory, share memory by communicating.
pradeep@LearnGo example %
So far we have written two programs,
pradeep@LearnGo example % ls
go.mod go.sum hello.go quote.go
pradeep@LearnGo example %
Go added the quote
module as a requirement, as well as a go.sum
file for use in authenticating the module.
When we ran go mod tidy
, it located and downloaded the rsc.io/quote
module that contains the package we imported. By default, it downloaded the latest version – v1.5.2.
With this quick introduction, we got Go installed and learned some of the basics.