为了账号安全,请及时绑定邮箱和手机立即绑定

在 Golang 中返回一个带有接收者的函数

在 Golang 中返回一个带有接收者的函数

Go
红糖糍粑 2023-03-29 17:11:56
我是高朗的新手。我正在尝试使用一个装饰器,它返回一个带有接收器的函数。我怎样才能做到这一点?type myStruct struct {    s string}func myFunc(s string) {    fmt.Println(s)}// Here I want to return a function with a receiverfunc (*myStruct) myDecorator(fn func(string)) (*myStruct)func(string){    return (ms *myStruct)func(s string) {        fn(ms+s)    }}func main() {    ms := myStruct{"Hello"}    // Some func is function with receiver as myStruct pointer    someFunc := myDecorator(myFunc)    // This is expected to print "Hello world"    ms.someFunc(" world")}
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 127 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信