英文:
go pass dynamically created function as parameter
问题
好的,以下是翻译好的内容:
好的。我有些困惑,不太明白这里关于"MyPrinter"的具体情况。
让我一步一步来(如果我理解错了,请纠正我):
- 创建了"Salute"结构体。
- 调用了"Greet"函数。
2.a 调用了"CreatePrinterFunction"函数,参数是字符串"!!!"。该函数返回一个"MyPrinter"(它是一个接受字符串参数并返回空的函数)。 - 变量"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>
- The "Salute" structure is created <br>
- 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> - 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 "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("!!!"))
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论