回答

收藏

函数可以作为参数传递吗?

技术问答 技术问答 334 人阅读 | 0 人回复 | 2023-09-12

在 Java 中,我可以做类似的事情
2 i: q$ ]' n! l" e5 U% `
    8 P! r/ I' t: f, F6 Y& ]9 I
  • derp(new Runnable { public void run * run this sometime later */ }code]以后操作方法中的代码。处理起来很痛苦(匿名内部类),但可以做到。, ~8 W) a' Y2 M) W: h8 p- \
  • Go 有什么能促进函数/回调作为参数传入吗?
    : c# r. N" Y7 ?3 G$ ]
  •                                                                
    2 B8 ~! Y$ J- [# B2 g( \
  •     解决方案:                                                               
    # f! G0 S1 ?1 F; l  X. ^" `
  •                                                                 是的,请考虑一些示例:x)    fmt.Println(result)    // Output: "1111011"    foo := func(x int) string { return "foo"    result = quote123(foo)    fmt.Println(result)    // Output: "foo"    _ = convert(foo) // confirm foo satisfies convert at runtime    // fails due to argument type    // _ = convert(func(x float64) string { return ""}code]                x) })    fmt.Println(result)    // Output: "1111011"    foo := func(x int) string { return "foo" }    result = quote123(foo)    fmt.Println(result)    // Output: "foo"    _ = convert(foo) // confirm foo satisfies convert at runtime    // fails due to argument type    // _ = convert(func(x float64) string { return "" })}! \1 d1 {6 [. E& O* i# h# e
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则