The three-dot ...
notation can be used in a few places:
arr := [...]string{"One", "Two", "Three", "Four", "Five"}
fmt.Println(len(arr)) // 5
Note that this creates an array with a fixed length, not a slice. So methods like append
will not work.
This is a function that can take any number of trailing arguments. Create a variadic function by prefixing the type with ...
like this:
func sum(nums ...int) int {
total := 0
for _, num := range nums {
total += num
}
return total
}
In this example, nums will be a slice
of ints
. You would call sum
like this:
total := sum(1, 2, 15, 24)
fmt.Println(total) // 42
You can also use the ...
a notation to pass a slice (not an array) into a variadic function like this:
slice := []int{2, 5, 9, 8}
total = sum(slice...)
fmt.Println(total) // 24
You'll most likely have seen or used this command already:
go test ./...
It simply means “test all packages in this directory (and subdirectories)”
Self taught software developer with 11 years experience excelling at JavaScript/Typescript, React, Node and AWS.
I love learning and teaching and have mentored several junior developers over my career. I find teaching is one of the best ways to solidify your own learning, so in the past few years I've been maintaining a technical blog where I write about some things that I've been learning.
I'm passionate about building a teams culture and processes to make it efficient and satisfying to work in. In many roles I have improved the quality and reliability of the code base by introducing or improving the continuous integration pipeline to include quality gates.