Learning Go (Part 1 - Getting Started)
Post Details
Description: A no-BS intro to the Go programming language — why you might want to learn it, how to install it, set up your editor, and write your first concurrent Go program using goroutines. Perfect for anyone getting started with Go and not in the mood for fluff.
Date: Jan 27, 2025
Word_Count: 801
What the fuck is Go?
Go is a general perpos programming language that is staticly typed and has sooo many fucking features. Even the guys at suckless will call it bloated but there are some good reasons to use it. It’s made but Robert Griesemer, Rob Pike and Ken Thompson. When you see these name you know the thing is going to be good.
Why learn go?
IDK you search for it. If you want to you should but if you don’t then it’s a no big deal. Learn something else IG.
But, jokes apart here are few reasons to learn Go:
- Performance: Since it’s a compiled language unlike Python or something even worse JavaScript, it runs much faster.
- Out of the box Concurrency Support: Go supports concurrency with the
help of
goroutines
and makes the code you write much better since “it’s not 1970s” that our computers only have a single core. It is capable of running multiple tasks simultaneously saving time and resources. - Simplicity: Its syntax is basically English and still manages to be so fucking fast. While some of the more advanced topics are can be intimidating, it still manages to keep the syntax as simple as it can.
- Good Eco-system: The standard library has everything you need, for building programs.
Enough blabbering and selling Go
to you. Here are some ways we can get started
writing Go
.
Setting up the environment
Install Go
Run this script or just manually install it from go.dev/doc/install. Following script will run for Linux, IDK about BSD (should run) but apart from that I ain’t supporting you guys find your own way of installing IG or just install Linux into your system.
Also be aware of what the script is doing, don’t just run random scripts you find on the internet. That’s how you fuck up you system.
1#!/bin/sh
2
3TMP_DIR=$(mktemp -d)
4URL="https://golang.org/dl/"
5TAR_FILE="go1.23.5.linux-amd64.tar.gz" # Update this to the latest version URL
6INSTALL_DIR="/usr/local"
7
8cleanup() {
9 rm -rf "$TMP_DIR"
10}
11
12trap cleanup EXIT
13
14echo "Downloading: $URL$TAR_FILE"
15
16curl -fsSL "$URL$TAR_FILE" -o "$TMP_DIR/$TAR_FILE"
17if [ $? -ne 0 ]; then
18 echo "Failed to download Go."
19 exit 1
20fi
21
22tar -C "$INSTALL_DIR" -xzf "$TMP_DIR/$TAR_FILE"
23if [ $? -ne 0 ]; then
24 echo "Error: Failed to extract Go."
25 exit 1
26fi
27
28echo "Setting up Go environment"
29export PATH="$INSTALL_DIR/go/bin:$PATH"
30
31echo "Go version:"
32go version
This should have the go version at the end, and it will be the version you mentioned in the URL.
Now set up an editor for Go.
We will use my Neovim config from
github.com/iamb4uc/dots that already has all
the basic options turned on for most of the programming language use mason to
install some Go plugins (open up mason using <leader>cm
).
Now you can write a real Go program natively in your system.
To do so, here’s an example for a basic goroutine
program.
Create a temp directory and make a project just for the time being:
1$ mktemp -d
2$ cd </path/to/temp/directory> # usually its /tmp/tmp.<something>
3$ touch main.go
Write this code, I’ll explain this in another blog, but this is a basic go
program with concurrency using goroutine
1package main
2
3import (
4 "fmt"
5 "time"
6)
7
8func greetings(s string) {
9 for i := 0; i < 100; i++ {
10 time.Sleep(time.Millisecond)
11 fmt.Printf("%v: %v\n", i, s)
12 }
13}
14
15func main() {
16 go greetings("Hi")
17 greetings("Mom")
18}
This code is a basic goroutine
implementation for 100 iteration.
Last step is execution of the code
To execute the commands we have some different tools:
go run <filename>
: It runs the program just like running python. IDK its internal working, but it’s kinda like a quick compiler that runs go code IG. It’s good for development in my opinion.go build .
: This actually compiles the code into an executable binary making it way more efficient when shipping out production code.gcc-go <filename>
: Same asgo build
it’s a GCC compiler instead of a native go compiler. But its unlike the go compiler make a lightweight executable with a smaller size usually.
Here is all the example for these commands:
1go run main.go # This runs the file as aspected
2go build main.go # This compiles the file into an executable e.g. main/main.exe
3gccgo main.go # This also complies the code into a less bloated executable
Difference between go build
and gccgo
when it comes to file size
1$ gccgo main.go -o main
2$ ll main
3-rwxr-xr-x 63k iamb4uc 28 Jan 20:32 main
4$ go build main.go
5$ ll main
6-rwxr-xr-x 2.1M iamb4uc 28 Jan 20:34 main
That’s all for this Part from next one we will be writing some proper go code.
For the time being you can check out A Tour Of Go