Go has a few for loop options: Classic for loop This format is common in many languages: The one difference in Go is the lack of opening parentheses around the init statements, which are required in other languages like Javascript, Java, or C. For loop using range Golang doesn’t have a for in loop, but […]
Golang
Golang Ellipsis
Golang’s ellipsis-…-has a few different uses. Defining variadic function parameters What is variadic? It is a function “of unknown arity”. In plain English, that means “a function that can take an unknown number of arguments”. A commonly-used function with variadic function parameters is Go’s builtin log.Printf. This is its signature: The signature of Printf says […]
Golang iota explained
If you’ve worked in Go, you’ve likely seen iota. Here’s how the Go docs explain it: Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants. – The Go Docs That sort of makes sense, but let me put it into plain English. iota is used when declaring a list of constants, […]
Pointers in Go
If you’re used to working in dynamic languages like Javascript, Ruby, or Python, Golang feels very different. One of the big differences is the explicit use of pointers. You might find yourself asking these questions: I have practical answers! Golang ampersand & and asterisk * Obtain an address with & Let’s start with &. It […]
Check if a key exists in a Golang map
Use an “index expression“. Buried in that official doc link is an example like this: In other languages, using index expressions (a term I just learned) could be risky if the key doesn’t exist: Instead of blowing up, or returning a generic (and possibly dangerous) value, Go gives back a second piece of information letting […]