5 small tips I recently learned in Go

Andrei Boar
3 min readJun 6, 2024

--

Intro

No matter how long you’ve been programming in your favorite language, you still find new small bits that rub you in a pleasant way.

Here are 5 small tips I recently learned in Go that I didn’t know existed.

Make the compiler count the array elements

You don’t use arrays much in Go. Instead, you use slices, but when you do, if you’re tired of writing the number of elements in the array yourself, you can use […] and the compiler will count them for you:

package main

import "fmt"

func main() {
arr := [3]int{1, 2, 3}
sameArr := [...]int{1, 2, 3} // Use ... instead of 3

// Arrays are equivalent
fmt.Println(arr)
fmt.Println(sameArr)
}

Use go run . instead of go run main.go

Whenever I start prototyping in Go, I like to start with a main.go file:

package main

import "fmt"

func main() {
sayHello()
}

func sayHello() {
fmt.Println("Hello!")
}

But when the main.go file gets big, I like to split my structs into separate files using the same main package. However, if I have the following files:

main.go

package main

func main() {
sayHello()
}

say_hello.go

package main

import "fmt"

func sayHello() {
fmt.Println("Hello!")
}

Typing go run main.go gives the following error:

# command-line-arguments
./main.go:4:2: undefined: sayHello

To solve this, use go run .

Check out this episode from Boldly Go for a full explanation. I learned this tip from its author Jonathan Hall.

Use the underscore to make your numbers readable

Did you know that you can use the underscore to make your long numbers easier to read?

package main

import "fmt"

func main() {
number := 10000000
better := 10_000_000

fmt.Println(number == better)
}

I found this tip while taking Miki Tebeka's Practical Go Foundations course on ArdanLabs. Miki is a wonderful instructor, and I'm looking forward to reading his latest book, Effective Go Recipes, where I’m sure I will find more tips like these.

You can have a different test package in the same package

In Go, I thought you could have only 1 package per folder. That’s not entirely true. For a package called yourpackage, you can have another package called yourpackage_test in the same directory, in which you can write your tests.

Why would you do that? You can write your tests using the same package yourpackage but when you do this, the unexported functions are available as well. By writing your tests in yourpackage_test, you make sure that you test only the exposed behavior.

I found this tip in 100 Go Mistakes, which is a wonderful book written by Teiva Harsanyi. Do yourself a favor and buy that book. Even after 6 years of working with Go, I learned a lot from it.

Passing the same argument multiple times

When using string formatting functions, I always found it annoying that I had to repeat an argument that was used multiple times:

package main

import "fmt"

func main() {
name := "Bob"
fmt.Printf("My name is %s. Yes, you heard that right: %s\n", name, name)
}

Apparently, there is a more convenient way. You can use pass it only once and use its order in the %s verb with %[order]s:

package main

import "fmt"

func main() {
name := "Bob"
fmt.Printf("My name is %[1]s. Yes, you heard that right: %[1]s\n", name)
}

I found this tip on Twitter by Jon Erickson:

I hope you learned something new today. How about you? Did you recently find some small Go tips that you never knew existed?

If you enjoyed this article, please give it a clap or connect with me on Linkedin to stay in touch. Happy coding!

--

--