我正在Go for Go中编写一个解析器,为了测试它,我从github项目中下载了一堆文件.
在https://github.com/andlabs/ui中,我碰到了包含这段代码的文件:
func moveLabel(*Button) {
from := movingCurrent
to := 0
if from == 0 {
to = 1
}
movingBoxes[from].Delete(0)
movingBoxes[to].Append(movingLabel, false)
movingCurrent = to
}
Run Code Online (Sandbox Code Playgroud)
有点让我感到困惑的是看到一个Button没有名字的指针作为函数参数,这使得无法从函数内部引用.
但是,鉴于编译器没有抱怨,它似乎在语法上是正确的.
Go中的unamed函数参数的目的是什么?
我知道如何用 javascript 来做,它会类似于:
Go 函数将接收一个函数作为参数,我想获取函数作为字符串来构建一个地图,然后将其保存在某个数据库中。
package main
import (
"fmt"
)
func describe(i interface{}) string {
return fmt.Sprintf("%v", i)
}
func dummyReducer(int) int {
return 1
}
func Accumulator(reducer func(int, int) int, init int) func(int) int {
input := map[string]interface{}{
"func_name": "accumulatorLoger",
"func_data": map[string]interface{}{
"reducer": string(describe(reducer)),
"init": init,
},
}
// {
// func_data: { init: 10, reducer: '0x64b880' },
// func_name: 'accumulatorLoger'
// }
// TODO: next: save the input data in the database
fmt.Println(input)
return dummyReducer
}
Run Code Online (Sandbox Code Playgroud)