Today I Learned About Golang
banner
til-about-golang.bsky.social
Today I Learned About Golang
@til-about-golang.bsky.social
Interesting bits and pieces, gotchas, things that are nice to know about the Go programming language.
In an anonymous function if a parameter is not used within the function the parameter name can be omitted
July 15, 2025 at 2:30 PM
During compilation you can set the GOAMD64 and GOARM64 environment variables to specify the minimum architecture requirements to your machines. The compiler will then generate instructions that are only executable on defined architectures or newer ones.
April 1, 2025 at 9:42 PM
In a hanging go program, you can get a stack dump of all your goroutines by using SIGABRT or SIGQUIT.

SIGQUIT can be invoked via CTRL + \.

If this doesn't work (because of other keyboard settings) you can also do killall -QUIT <processname> or killall -ABRT <processname>.
March 1, 2025 at 2:13 PM
Goroutines have a stack which can grow. This differs from other programming languages (like C f.e.) where the stack size is fixed. The stack memory of a goroutine comes from the process heap. The stack memory allocation happens in the userspace, managed by the go runtime.
February 20, 2025 at 7:02 AM
In Go, the TZ environment variable is used to set the local timezone. If the variable is set, go attempts to load the respective timezone data.

This can be provided via a path in ZONEINFO environment variable, some standard OS locations (f.e. /usr/share/zoneinfo on UNIX).
February 13, 2025 at 8:14 PM
Go maps do not shrink in size in terms of allocated memory, even if you delete the elements from the map.

If you use pointers as map values and then delete the elements, the memory for the values will be freed, but the memory for the map itself will still be allocated.
February 4, 2025 at 12:00 PM
It is possible to use index expressions on array pointers without dereferencing them:

a := &[3]string{"a", "b", "c"}
fmt.Println(a[1])

It is not possible to do the same thing with slices.
January 7, 2025 at 9:34 PM
In Go, it is possible to create a nil slice using type conversion. It works like this:
g := []string(nil)

In this example nil is converted to the type []string, e.g. a slice of strings
December 27, 2024 at 2:13 PM
In go when we call the defer statement, a _defer object is created. It can be allocated on the heap, stack or be a so-called open coded defer.
December 20, 2024 at 10:21 PM
Pointers can point to other pointers in Golang. The following code will print 5:

var number = 5
var pointer *int = &number
var pointerToPointer **int = &pointer

fmt.Println(**pointerToPointer)

Note the **int type of the pointerToPointer variable.
December 12, 2024 at 6:55 PM
A nil pointer in Go does not point to any memory address.
December 6, 2024 at 7:48 PM
It is possible to provide a capacity while initializing a map by providing a second argument to the make function in Go. It is however not possible to retrieve the capacity of a map using the cap function.
November 29, 2024 at 9:35 PM
The %q formatting directive, which is usually used to enquote a string, can be used to identify unprintable byte sequences.

For example:

test := "\xdc"
fmt.Printf("%q", test)

will print the value of test in quotes.

If we change the value of test to "\x64", "d" will be printed.
November 28, 2024 at 5:59 PM
Since a string in Go is just a slice of bytes, indexing a string in go returns a byte.

A for key, value := range loop, however, retrieves runes as values and byte starting positions as keys.

Further read: go.dev/blog/strings
Strings, bytes, runes and characters in Go - The Go Programming Language
How strings work in Go, and how to use them.
go.dev
November 28, 2024 at 5:39 PM
In Go you can declare typed and untyped constants.

const untyped = "abc"
const typed string = "def"

Untyped constants can be not only assigned to string variables, but also to variables of a type which is a string alias.

More on this in the golang blog: go.dev/blog/constants
Constants - The Go Programming Language
An introduction to constants in Go.
go.dev
November 26, 2024 at 7:55 PM
A nil channel is never ready for communication. This means that receiving from and sending to a nil channel blocks forever.

Executing a code which sends to or receives from a nil channel results in a deadlock error.
November 25, 2024 at 7:43 PM
In Go, if you use a pointer receiver, only pointer types implement the interface. On the other hand, if you use a value receiver, both pointer and non-pointer types implement the interface.
November 24, 2024 at 7:16 PM
Appending to a slice in golang does not create a new slice, even when we thereby create a new variable. This can lead to unwanted side effects.
November 24, 2024 at 6:45 PM