In 'A tour of Go' there is such phrase
If the top-level type is just a type name, you can omit it from the elements of the literal.
I'm new in Go so I'm curious when can not it be omitted?
var m = map[string]Vertex{
"Bell Labs": {40.68433, -74.39967}, //top-level type is omitted
"Google": {37.42202, -122.08408},
}
As mentioned by commenter @TimCooper, if Vertex
were an interface type then you would need to explicitly name the concrete type which implements the interface since the compiler couldn't reasonably guess which implementation you are referring to, for example:
type NoiseMaker interface { MakeNoise() string }
type Person struct {}
func (p Person) MakeNoise() string {
return "Hello!"
}
type Car struct {}
func (c Car) MakeNoise() string {
return "Vroom!"
}
// We must provide NoiseMaker instances here, since
// there is no implicit way to make a NoiseMaker...
noisemakers := map[string]NoiseMaker{
"alice": Person{},
"honda": Car{},
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With