Collapsing like function parameters

March 31, 2023

Yesterday we learned that we may omit function parameter and result names in a function type definition. But this poses a small limitation:

Function types

… If [the name is] absent, each type stands for one item of that type.

What does that mean, exactly?

Well, when using named parameters and results, we have the option to omit the type for subesquent arguments of the same type. Here’s an example to make it more clear;

func(a int, b int, c int) // Can be shortened to:
func(a, b, c int)

This shortening is not possible with unnamed parameters. Instead, you must specify the type for each argument, as follows:

func(int, int, int)

Of course, there’s still the option to use the blank identifier, if you prefer:

func(_, _, _ int)

Quotes from The Go Programming Language Specification Version of December 15, 2022

Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .