Go API Specification Related Ques

Hello,

I have been working on Go for last 6-7 months now and I started reading Go docs in depth recently.

I came across a section on Functions. I have normally used functions with proper parameters and variadic params too. But, I haven’t used functions in following format:

https://golang.org/ref/spec#Function_types
A) func(a, _ int, z float32) bool

  • The second param is _ which I do not know what it mean here. I know we use _ with range or with multiple returns but I am confused to see this in functions. Could anyone please give me an example of this and explain a bit?

B) func(int, int, float64) (float64, *[]int)

  • No variable names??

Also, in Map section, I am looking for an example for the following:
map[*T]struct{ x, y float64 }

So far, I have used maps with int or string types only.

Thanks

a) function requires 3 arguments, but middle one is ignored, so not used in the function body. It is maybe made to satisfy some interface or maybe just a bad signature.

b) This is just signature. You probably found it in some interface definition. You don’t need variable names there, just types.

c) Just a map of structs. You can have maps of any type.

1 Like

On b), here is an example
https://play.golang.org/p/BRaxaAFek3w

1 Like

On c), the key for a map must have a definition of equality.

see the excellent article here:
https://blog.golang.org/go-maps-in-action

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.