Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If mentioning <code>var</code> is your main problem, you can drop it easily, by changing <code>=</code> into <code>:=</code>, like this:</p> <pre><code>english := Greeting(func(name string) string { return ("Hello, " + name); }) </code></pre> <p>But you don't even have to cast your function into <code>Greeting</code>. The spec says this about <a href="http://weekly.golang.org/doc/go_spec.html#Function_types" rel="noreferrer">function types</a>:</p> <blockquote> <p>A function type denotes the set of all functions with the same parameter and result types.</p> </blockquote> <p>And this about <a href="http://weekly.golang.org/doc/go_spec.html#Type_identity" rel="noreferrer">type identity</a>:</p> <blockquote> <p>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.</p> </blockquote> <p>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 <code>type Greeting func...</code> you're just giving a name to a particular function type, not defining a new one.</p> <p>So the following code works, and I hope shows the right way to work with function types in Go:</p> <pre><code>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") } </code></pre> <p>Notice that I also dropped semicolon and parenthesis from your <code>english</code> function. Go developers don't use these punctuations if they don't have to.</p> <p><strong>UPDATE:</strong> Now that you've provided a sample code I can clearly understand the problem.</p> <p>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:</p> <pre><code>english := func(name string) string { return "Hello, " + name } Greeting(english).exclamation("ANisus") </code></pre> <p>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.</p> <p>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:</p> <pre><code>package main import "fmt" type Greeting struct { say func(name string) string } func newGreeting(f func(string) string) *Greeting { return &amp;Greeting{say: f} } func (g *Greeting) exclamation(name string) string { return g.say(name) + "!" } func main() { english := &amp;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")) } </code></pre> <p>Here <code>english</code> and <code>french</code> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload