Back to Writing
Post Series

Idiomatic Go: Practical Error Handling Patterns

Aug 10, 2026• 4 MIN READ

This week I am starting with reading a go idiomatic book named (An Idiomatic Approach to Real-World Go Programming), and trying to explain here whatever things (new or old) I have learnt.

Let’s start with the first Chap:

While working with any language we need an environment (requiring what version of that language we want to use, additional dependencies and all). Likewise, we need an environment for golang (Go).

Installation of Go

Windows: Although you can directly install it from it’s official website, but I recommend using .msi installer.

Mac: Two very common and efficient ways are install it via Homebrew (brew install go) or .pkg installer.

Linux: As I am a linux user (Currently using CachyOS based on Arch Linux), I highly recommend using the official package manager (pacman) to install Go and then we can set it’s path according to our shell (bash/zsh) executable path.

for path issue in windows, setx GOPATH %USERPROFILE%\go setx path “%path%;%USERPROFILE%\bin”

Note: You may need to restart your shell after setting the path.

– One interesting thing I learned is that Go compile in a single binary file and doesn’t need any additional software installed to run them.–

Go provides us a lot of disparate tools and libraries that we can use to build our applications according to our needs. We can use go command to install these tools and libraries.

Example:

go get github.com/golang.org/x/tools/cmd/goimports

Similarly we have two other commands go build and go run.

Let’s see how they differ from each other. go run: As go is a compiled language, the go run code will definitely compiles our code by building the binary followed by executing it and subsequently running and deleting the binary from the directory. go build: The go build command compiles our code into a binary file but does not run it. It is useful when we want to distribute our application as a standalone executable.The name of the binary file matches the name of the package or file you passed it. But, if you do want to name it something of your choice, you can use the -o flag.

go build -o your_binary_name actual_file.go

You can use the below code and try to run it with first go run and then go build. See whatever I told above is true or not.

package main
import "fmt"
func main() {
 fmt.Println("Hello, world!")
}

Publishing Go Code

The method of publishing or distributing the go code is quiet different from the other languages. For instance: Maven Central for Java, NPM registry for JavaScript, or the CRAN for R. Instead we share go code through GitHub or other version control systems (repositories). Go install command takes an argument, which is the location of the source code repository of the project you want to install. It then downloads, compiles, and installs the tool into your $GOPATH/bin directory.

Formatting and Linting

Go has a built-in formatter (gofmt) and linter (golint) that can help you write clean and idiomatic Go code. You can run these tools manually or integrate them into your development workflow using tools like go fmt and golint. It’s a good practice to always run these tools before committing your code. I know it’s a bit tedious and nasty task to do, but it’s actually worth it when you work on some real world issue or repositories in such large amount of files where each files are massive, these tools are the ones who’s gonna save your life but only if you are used to using them.

There are also some other more efficient liniting tool. While the golint is mainly used to ensure your code follows style guidelines. Some of the changes it suggests include properly naming variables, formatting error messages, and placing comments on public methods and types. These aren’t errors; they don’t keep your programs from compiling or make your program run incorrectly, the go vet which along with these detects code is syntactically valid, but there are mistakes that are not what you meant to do. This includes things like passing the wrong number of parameters to formatting methods or assigning values to variables that are never used.

Note: We knew that we don’t need semicolon to type go code but wonder why only this passes

func main(){
 fmt.Println("Hello, world!")
}

and this one fails

func main()
{
 fmt.Println("Hello, world!")
}

I learnt an interesting thing, that go actually requires the semicolon just like in C/C++. However, we don’t need to explicitly type it like in other porgramming languages. It automatically add the semicolon at compile time by considering few things - • An identifier (which includes words like int and float64) • A basic literal such as a number or string constant • One of the tokens: “break,” “continue,” “fallthrough,” “return,” “++,” “–,” “),” or “}”

Now if we look at our above issue then we realise why it is causing an error.

func main();
{
 fmt.Println("Hello, world!");
};