将动态创建的函数作为参数传递

huangapple go评论139阅读模式
英文:

go pass dynamically created function as parameter

问题

好的,以下是翻译好的内容:

好的。我有些困惑,不太明白这里关于"MyPrinter"的具体情况。
让我一步一步来(如果我理解错了,请纠正我):

  1. 创建了"Salute"结构体。
  2. 调用了"Greet"函数。
    2.a 调用了"CreatePrinterFunction"函数,参数是字符串"!!!"。该函数返回一个"MyPrinter"(它是一个接受字符串参数并返回空的函数)。
  3. 变量"message"和"defaultMessage"被设置为字符串。

现在问题来了,我不明白那些"do("str")"到底在做什么。

package main
import "fmt"

type Salute struct {
    name     string
    greeting string
}

type MyPrinter func(s string) ()

func Greet(salute Salute, do MyPrinter) {
    message, defaultMessage := CreateMessage(salute.name, salute.greeting, "noName")
    do(message)
    do(defaultMessage)
}

func Print(s string) {
    fmt.Print(s)
}
func PrintLine(s string) {
    fmt.Println(s)
}

func CreatePrinterFunction(custom string) MyPrinter {
    return func(s string) {
        fmt.Println(s + custom)
    }
}

func CreateMessage(name string, greeting ...string) (message string, defaultMessage string) {
    message = name + " " + greeting[0]
    defaultMessage = "hey " + name
    return
}

func main() {
    var s = Salute{"Joe", "hello"}
    // Greet(s, PrintLine)
    Greet(s, CreatePrinterFunction("!!!"))
}

希望这能帮到你!

英文:

Ok. I have some trouble understanding what exactly is going on here with "MyPrinter"
Let me go step by step (please correct me if got something wrong) <br>

  1. The "Salute" structure is created <br>
  2. Call to "Greet" function <br>
    2.a Call to "CreatePrinterFunction" with the strgin "!!!". This function returns a "MyPrinter" (witch is a function that takes in a string and returns nothing)<br>
  3. the variables "message" and "defaultMessage" are set with the strings<br>

Now here's the problem, I don't understand what exactly are those do("str") doing

package main
import &quot;fmt&quot;

type Salute struct {
	name string
	greeting string
}

type MyPrinter func (s string) ()

func Greet(salute Salute, do MyPrinter) {
	message, defaultMessage := CreateMessage(salute.name, salute.greeting, &quot;noName&quot;)
	do(message)
	do(defaultMessage)
}

func Print(s string) {
	fmt.Print(s)
}
func PrintLine(s string) {
	fmt.Println(s)
}

func CreatePrinterFunction(custom string) MyPrinter {
	return func (s string) {
		fmt.Println(s + custom)
	}
}

func CreateMessage(name string, greeting ...string) (message string, defaultMessage string) {
	message = name + &quot; &quot; + greeting[0]
	defaultMessage = &quot;hey &quot; + name
	return
}

func main() {
	var s = Salute{&quot;Joe&quot;, &quot;hello&quot;}
    // Greet(s, PrintLine)
    Greet(s, CreatePrinterFunction(&quot;!!!&quot;))
}

答案1

得分: 2

CreatePrinterFunction 返回一个函数字面量

return func (s string) {
    fmt.Println(s + custom)
}

该函数字面量实现了 MyPrinter 接口,该接口由任何接受一个字符串参数并返回空的函数实现:

type MyPrinter func(s string)

(请注意,提供的代码片段中的 MyPrinter 定义在末尾包含了一个无用的 ()

然后,实现了 MyPrinter 接口的创建的函数被作为 Greet 函数的 do 参数传递:

func Greet(salute Salute, do MyPrinter) {

Greet 内部的代码运行 do(message) 时,创建的函数字面量被调用,进而运行了 fmt.Println(message + custom) 的等效操作。

这是一种相当复杂的方式来完成简单的任务。 将动态创建的函数作为参数传递

英文:

CreatePrinterFunction returns a function literal:

return func (s string) {
    fmt.Println(s + custom)
}

That function literal implements the MyPrinter interface, which is an interface implemented by any function that takes a string argument and returns nothing:

type MyPrinter func(s string)

(note that the MyPrinter definition in the provided snippet includes an extra () at the end which does nothing)

Then, that function created which implements the MyPrinter interface is passed as the do parameter of the Greet function:

func Greet(salute Salute, do MyPrinter) {

When code inside Greet runs do(message), the created function literal is called, which in its turn runs the equivalent of fmt.Println(message + custom).

It's a pretty convoluted way to do something simple. 将动态创建的函数作为参数传递

huangapple
  • 本文由 发表于 2013年8月31日 02:22:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/18539219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定