Learning things is fun so I've decided it's time to learn go and because I enjoy making these little series I thought I'd write about it too.
My first step was to Google 'learning Go', which took me to their getting started page. I'll be using WSL to execute my code, so let's find the install instructions.
I've not installed Go on my system before, so I don't need to remove an existing installation. I can just download the latest Go files and extract them into /usr/local
.
wget https://go.dev/dl/go1.24.1.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.24.1.linux-amd64.tar.gz
Now we need to add the Go binaries to our $PATH
variable so we can execute it from anywhere. We should test that Go can be called by running go version
at this point.
echo "export PATH=/usr/local/go/bin:\$PATH" >> ~/.bashrc
source ~/.bashrc
go version
Don't forget to tidy up as you go.
rm go1.24.1.linux-amd64.tar.gz
Compared to setting up some other languages, this was refreshingly simple. No weird dependencies or long-winded setups, just download, extract, and go!
Go by Example
Go by Example is a hands-on introduction to Go using annotated example programs. It’s a great starting point to use when tackling any Go project.
There are a few tutorial types available. I've decided to go with "Go by Example" and if I ever get lost I'll head on over to documentation to get unstuck.
Hello, World!
No learning journey is complete without printing 'Hello, World!', it's the rite of passage for every programmer. Let's see what it looks like in Go.
This first section is mostly about executing code, either by running it directly or building it as a binary. But we also get to see our first import and function.
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
We're importing fmt, which the docs tell me is a string formatting package, and creating a main function as the program's entry point.
To run the program, we simply write go run
followed by the location of our program file.
go run ./hello-world.go
To build the program, we do the same but swap run
for build
. Instead of executing the script directly, this creates a new file we can run without go run
.
go build ./hello-world.go
./hello-world
Values
Next up, we're looking at different value types. We have string
s, which can be appended using +
; integer
s and float
s, which support standard math operators; and boolean
s, which work exactly as expected
package main
import "fmt"
func main() {
fmt.Println("go" + "lang")
fmt.Println("1+1 =", 1+1)
fmt.Println("7.0/3.0 =", 7.0/3.0)
fmt.Println(true && false)
fmt.Println(true || false)
fmt.Println(!true)
}
Variables and Constants
I've decided to cover variables and constants together since, in my mind, they're pretty similar but we'll see if that holds true.
Variables
To declare a variable we can use the var
keyword, interestingly we can declare several variables in one go, though I can imagine this getting hard to read.
Types are inferred, but we can also specify a type and leave the value blank, which initialises the variable as zero-valued
. I really like this, it's very developer-friendly. We can have an 'empty' variable that won't throw errors when used.
There's also a shorthand syntax for variable declaration: :=
. Instead of writing var age int = 34
, we can simply write age := 34
.
package main
import "fmt"
func main() {
var a = "initial"
fmt.Println(a)
var b, c int = 1, 2
fmt.Println(b, c)
var d = true
fmt.Println(d)
var e int
fmt.Println(e)
f := "apple"
fmt.Println(f)
}
Constants
Finally, let's look at constants. A constant can be used anywhere a variable can, but unlike variables, constants cannot be reassigned. It's generally best practice to use constants where possible, as they offer a whole bunch of compile-time optimisations.
As an aside, we're also importing our second package, the math, which provides a range of mathematical functions
package main
import (
"fmt"
"math"
)
const s string = "constant"
func main() {
fmt.Println(s)
const n = 500000000
const d = 3e20 / n
fmt.Println(d)
fmt.Println(int64(d))
fmt.Println(math.Sin(n))
}
Signing off
Thank you for coming on this little adventure with me. I'd like to carry on with this series for a little while so if you'd like to follow along, you can press the 'follow' button to be notified of new posts.
Feel free to leave any feedback. If you're also learning in public, drop a link to your series in the comments, I'd love to check it out
Thanks for reading! If you'd like to connect, here are my Twitter, BlueSky, and LinkedIn profiles. Come say hi 😊
Top comments (2)
So far it feels like a refreshing change but we've not really covered anything complex yet so there's still time for it to all go wrong 😅
Thanks for making things easy :))