我正在尝试将以下 Python 功能移植到 Golang。特别是如何将函数存储在切片中然后调用它们。我怎样才能在 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
catspeake
九州编程
慕村9548890
相关分类