在Go语言中,可以在回调声明中评估变量。

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

golang: evaluate variable in callback declaration

问题

我正在尝试在Go语言中定义一个回调函数:

package main

func main() {
    x, y := "old x ", "old y"
    callback := func() { print("callback: ", x, y, "\n") }
    callback_bound := func(a, b string) func() {
        return func() { print("callback_bound: ", a, b, "\n") }
    }(x, y)
    callback_hacked := func() { print("callback_hacked: ", "old x ", "old y", "\n") }

    x, y = "new x ", "new y"

    callback()
    callback_bound()
    callback_hacked()
}

输出结果为:

callback: new x new y
callback_bound: old x old y
callback_hacked: old x old y

基本情况下是可以工作的,但它会使变量未绑定,即在调用时使用的是值。毫无疑问,这在某些情况下是有用的,但我该如何声明callback_bound,以便在声明时使用变量的值,使输出变为:

callback: new x new y
callback_bound: old x old y
callback_hacked: old x old y
英文:

I am trying to define a callback in golang:

package main

func main() {
	x, y := "old x ", "old y"
	callback         := func() { print("callback: "        , x        , y       , "\n") }
	callback_bound   := func() { print("callback_bound: "  , x        , y       , "\n") }
	callback_hacked  := func() { print("callback_hacked: " , "old x " , "old y" , "\n") } 

	x, y = "new x ", "new y"

	callback()
	callback_bound()
	callback_hacked()
}

The output is:

callback: new x new y
callback_bound: new x new y
callback_hacked: old x old y

The basic case works, but it leaves the variables unbound, i.e. the values at call-time are used. No doubt, this is useful in some cases, but how do I declare callback_bound so that the values at declaration-time are used and the output becomes:

callback: new x new y
callback_bound: old x old y
callback_hacked: old x old y

答案1

得分: 2

例如,

package main

func callbackXY(x, y string) func() {
    return func() { print("callbackXY: ", x, y, "\n") }
}

func main() {
    x, y := "old x ", "old y"
    callback := callbackXY(x, y)
    x, y = "new x ", "new y"
    callback()
}

输出:

callbackXY: old x old y

或者

package main

func main() {
    x, y := "old x ", "old y"
    callback := func() {}
    {
        x, y := x, y
        callback = func() { print("callbackXY: ", x, y, "\n") }
    }
    x, y = "new x ", "new y"
    callback()
}

输出:

callbackXY: old x old y
英文:

For example,

package main

func callbackXY(x, y string) func() {
	return func() { print("callbackXY: ", x, y, "\n") }
}

func main() {
	x, y := "old x ", "old y"
	callback := callbackXY(x, y)
	x, y = "new x ", "new y"
	callback()
}

Output:

callbackXY: old x old y

Or

package main

func main() {
	x, y := "old x ", "old y"
	callback := func() {}
	{
		x, y := x, y
		callback = func() { print("callbackXY: ", x, y, "\n") }
	}
	x, y = "new x ", "new y"
	callback()
}

Output:

callbackXY: old x old y

答案2

得分: -1

bound := func(xx, yy int) { return func() { fmt.Println(xx, yy) } }(x, y)

未经测试。

请查看(或两次)http://golang.org/doc/effective_go.html

英文:
bound := func(xx,yy int) { return func(){fmt.Println(xx,yy)} }(x,y)

Untested.

Please have a look (or two) at http://golang.org/doc/effective_go.html

huangapple
  • 本文由 发表于 2014年3月3日 08:07:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/22135486.html
匿名

发表评论

匿名网友

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

确定