英文:
How to store functions in a slice in Go
问题
我正在尝试将以下Python功能移植到Golang中。特别是如何将函数存储在切片中并调用它们。在Golang中我该如何做到这一点?
package main
import "fmt"
type Dispatcher struct {
listeners []func()
}
func (d *Dispatcher) addListener(listener func()) {
d.listeners = append(d.listeners, listener)
}
func (d *Dispatcher) notifyUpdate() {
for _, f := range d.listeners {
f()
}
}
func beeper() {
fmt.Println("beep...beep...beep")
}
func pinger() {
fmt.Println("ping...ping...ping")
}
func main() {
dispatch := &Dispatcher{}
dispatch.addListener(beeper)
dispatch.addListener(pinger)
dispatch.notifyUpdate()
}
输出:
beep...beep...beep
ping...ping...ping
以上是将Python代码转换为Golang的示例。在Golang中,我们使用函数类型的切片来存储函数,并通过循环调用它们。
英文:
I'm trying to port the following Python functionality to Golang.
Especially, how to store functions in a slice and then call them.
How can I do this in Golang?
class Dispatcher(object):
def __init__(self):
self._listeners = []
def addlistener(self, listener):
self._listeners.append(listener)
def notifyupdate(self):
for f in self._listeners:
f()
def beeper():
print "beep...beep...beep"
def pinger():
print "ping...ping...ping"
dispatch = Dispatcher()
dispatch.addlistener(beeper)
dispatch.addlistener(pinger)
dispatch.notifyupdate()
output:
beep...beep...beep
ping...ping...ping
答案1
得分: 22
这是一个相当简单的代码:
package main
import "fmt"
func main() {
var fns []func()
fns = append(fns, beeper)
fns = append(fns, pinger)
for _, fn := range fns {
fn()
}
}
func beeper() {
fmt.Println("beep-beep")
}
func pinger() {
fmt.Println("ping-ping")
}
Playground: http://play.golang.org/p/xuDsdeRQX3.
英文:
It's pretty easy actually:
package main
import "fmt"
func main() {
var fns []func()
fns = append(fns, beeper)
fns = append(fns, pinger)
for _, fn := range fns {
fn()
}
}
func beeper() {
fmt.Println("beep-beep")
}
func pinger() {
fmt.Println("ping-ping")
}
Playground: http://play.golang.org/p/xuDsdeRQX3.
答案2
得分: 1
以下是翻译好的内容:
或者,如果你想要一个更接近的结构(诚然,在这种情况下并不需要):
package main
import "fmt"
type dispatcher struct {
listeners []func()
}
func (d *dispatcher) addListener(f func()) {
d.listeners = append(d.listeners, f)
}
func (d *dispatcher) notify() {
for _, f := range d.listeners {
f()
}
}
func ping() {
fmt.Println("Ping... ping...")
}
func beep() {
fmt.Println("Beep... beep...")
}
func main() {
d := dispatcher{}
d.addListener(ping)
d.addListener(beep)
d.notify()
}
英文:
Alternatively, if you want an even closer structure (admittedly, not needed, at all, in this case):
package main
import "fmt"
type dispatcher struct {
listeners []func()
}
func (d *dispatcher) addListener(f func()) {
d.listeners = append(d.listeners, f)
}
func (d *dispatcher) notify() {
for _, f := range d.listeners {
f()
}
}
func ping() {
fmt.Println("Ping... ping...")
}
func beep() {
fmt.Println("Beep... beep...")
}
func main() {
d := dispatcher{}
d.addListener(ping)
d.addListener(beep)
d.notify()
}
答案3
得分: 0
package main
import "fmt"
func main() {
var funcs = []func(){}
funcs = append(funcs, beeper)
funcs = append(funcs, pinger)
for _, f := range funcs {
f()
}
}
func beeper() {
fmt.Println("I'm a beeper")
}
func pinger() {
fmt.Println("I'm a pinger")
}
点击此处查看代码。
英文:
package main
import "fmt"
func main() {
var funcs = []func(){}
funcs = append(funcs, beeper)
funcs = append(funcs, pinger)
for _, f := range funcs {
f()
}
}
func beeper() {
fmt.Println("I'm a beeper")
}
func pinger() {
fmt.Println("I'm a pinger")
}
答案4
得分: 0
func main(){
var funcSlice []func()
funcSlice = append(funcSlice, f1)
funcSlice = append(funcSlice, f2)
}
func f1(){
fmt.Println("")
}
func f2(){
fmt.Println("")
}
英文:
func main(){
var funcSlice []func()
funcSlice = append(funcSlice, f1)
funcSlice = append(funcSlice, f2)
}
func f1(){
fmt.Println("")
}
func f2(){
fmt.Println("")
}
答案5
得分: 0
如果你使用更复杂的函数,你可以这样做:
func addr(instru, beforeg [4]int) [4]int {
beforeg[instru[3]] = beforeg[instru[1]] + beforeg[instru[2]]
return beforeg
}
func addi(instru, beforeg [4]int) [4]int {
beforeg[instru[3]] = beforeg[instru[1]] + instru[2]
return beforeg
}
func day16Run(isTest bool) {
arrayFunc := [16]func([4]int, [4]int) [4]int{addr, addi}
// 使用 arrayFunc
}
如果你的函数具有不同的签名,可以使用切片并在需要时进行追加,但在调用函数时要小心:
https://stackoverflow.com/questions/37314365/golang-how-do-you-create-a-slice-of-functions-with-different-signatures
英文:
If you use more complexes functions you do this :
func addr(instru, beforeg [4]int) [4]int {
beforeg[instru[3]] = beforeg[instru[1]] + beforeg[instru[2]]
return beforeg
}
func addi(instru, beforeg [4]int) [4]int {
beforeg[instru[3]] = beforeg[instru[1]] + instru[2]
return beforeg
}
func day16Run(isTest bool) {
arrayFunc:= [16]func([4]int, [4]int) [4]int{addr, addi}
// use arrayFunc
}
And if your functions have differnt signatures, use a slice and append as you go, but be careful when calling the functions :
https://stackoverflow.com/questions/37314365/golang-how-do-you-create-a-slice-of-functions-with-different-signatures
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论