If mentioning var is your main problem, you can drop it easily, by changing = into :=, like this:
english := Greeting(func(name string) string {
return ("Hello, " + name);
})
But you don't even have to cast your function into Greeting. The spec says this about function types:
A function type denotes the set of all functions with the same parameter and result types.
And this about type identity:
Two function types are identical if they have the same number of parameters and result values, corresponding parameter and result types are identical, and either both functions are variadic or neither is. Parameter and result names are not required to match.
This means that each function has its own function type. If two functions have the same signature (parameter and result types), they share one function type. By writing type Greeting func... you're just giving a name to a particular function type, not defining a new one.
So the following code works, and I hope shows the right way to work with function types in Go:
package main
import "fmt"
type Greeting func(name string) string
func say(g Greeting, n string) { fmt.Println(g(n)) }
func french(name string) string { return "Bonjour, " + name }
func main() {
english := func(name string) string { return "Hello, " + name }
say(english, "ANisus")
say(french, "ANisus")
}
Notice that I also dropped semicolon and parenthesis from your english function. Go developers don't use these punctuations if they don't have to.
UPDATE: Now that you've provided a sample code I can clearly understand the problem.
For this purpose your code is good enough and there are not much other ways of doing it. If you like you can cast just before calling the method:
english := func(name string) string { return "Hello, " + name }
Greeting(english).exclamation("ANisus")
But I'm not sure this is an improvement. I'm just saying that for what you want to do there does not seem to be other ways to write the code.
That is, if we don't want to change your types. I mean, the whole idea of calling a method on a function type seems a little weird. Not that it's wrong, but a little rare. Another way of achieving the same effect in a more usual way is through a struct type and having a field for the function. Something like this:
package main
import "fmt"
type Greeting struct {
say func(name string) string
}
func newGreeting(f func(string) string) *Greeting {
return &Greeting{say: f}
}
func (g *Greeting) exclamation(name string) string { return g.say(name) + "!" }
func main() {
english := &Greeting{say: func(name string) string {
return "Hello, " + name
}}
french := newGreeting(func(name string) string {
return "Bonjour, " + name
})
fmt.Println(english.exclamation("ANisus"))
fmt.Println(french.exclamation("ANisus"))
}
Here english and french show two different ways of coding the same thing. Again, I'm not saying that this is the better solution, but a more usual and more flexible way of achieving the same effect.
varbecause you are declaringenglishas a global variable.