Go函数映射

huangapple go评论137阅读模式
英文:

Go map of functions

问题

我有一个Go程序,其中定义了一个函数。我还有一个应该为每个函数设置一个键的映射。我该如何做到这一点?

我尝试了以下方法,但它不起作用。

  1. func a(param string) {
  2.  
  3. }
  4.  
  5. m := map[string] func {
  6. 'a_func': a,
  7. }
  8.  
  9. for key, value := range m {
  10. if key == 'a_func' {
  11. value(param)
  12. }
  13. }
英文:

I have Go program that has a function defined. I also have a map that should have a key for each function. How can I do that?

I have tried this, but this doesn't work.
<pre>
func a(param string) {

}

m := map[string] func {
'a_func': a,
}

for key, value := range m {
if key == 'a_func' {
value(param)
}
}
</pre>

答案1

得分: 85

你是否想要做类似这样的事情?我已经修改了示例,使用了不同类型和数量的函数参数。

  1. package main
  2. import "fmt"
  3. func f(p string) {
  4. fmt.Println("函数 f 的参数:", p)
  5. }
  6. func g(p string, q int) {
  7. fmt.Println("函数 g 的参数:", p, q)
  8. }
  9. func main() {
  10. m := map[string]interface{}{
  11. "f": f,
  12. "g": g,
  13. }
  14. for k, v := range m {
  15. switch k {
  16. case "f":
  17. v.(func(string))("一个字符串")
  18. case "g":
  19. v.(func(string, int))("一个字符串", 42)
  20. }
  21. }
  22. }
英文:

Are you trying to do something like this? I've revised the example to use varying types and numbers of function parameters.

  1. package main
  2. import &quot;fmt&quot;
  3. func f(p string) {
  4. fmt.Println(&quot;function f parameter:&quot;, p)
  5. }
  6. func g(p string, q int) {
  7. fmt.Println(&quot;function g parameters:&quot;, p, q)
  8. }
  9. func main() {
  10. m := map[string]interface{}{
  11. &quot;f&quot;: f,
  12. &quot;g&quot;: g,
  13. }
  14. for k, v := range m {
  15. switch k {
  16. case &quot;f&quot;:
  17. v.(func(string))(&quot;astring&quot;)
  18. case &quot;g&quot;:
  19. v.(func(string, int))(&quot;astring&quot;, 42)
  20. }
  21. }
  22. }

答案2

得分: 80

m := map[string]func(string, string)

如果你知道签名(并且所有的函数都有相同的签名),这个方法是可行的。
我认为这比使用接口更清晰/更安全。

英文:
  1. m := map[string]func(string, string)

Works if you know the signature (and all the funcs have the same signature)
I think this is cleaner/safer than using interface{}

答案3

得分: 24

你可以定义一个类型,如果函数具有相同的接口。

  1. package main
  2. import "log"
  3. type fn func (string)
  4. func foo(msg string) {
  5. log.Printf("foo! Message is %s", msg)
  6. }
  7. func bar(msg string) {
  8. log.Printf("bar! Message is %s", msg)
  9. }
  10. func main() {
  11. m := map[string] fn {
  12. "f": foo,
  13. "b": bar,
  14. }
  15. log.Printf("map is %v", m)
  16. m["f"]("Hello")
  17. m["b"]("World")
  18. }
英文:

You can define a type if functions are same interface.

  1. package main
  2. import &quot;log&quot;
  3. type fn func (string)
  4. func foo(msg string) {
  5. log.Printf(&quot;foo! Message is %s&quot;, msg)
  6. }
  7. func bar(msg string) {
  8. log.Printf(&quot;bar! Message is %s&quot;, msg)
  9. }
  10. func main() {
  11. m := map[string] fn {
  12. &quot;f&quot;: foo,
  13. &quot;b&quot;: bar,
  14. }
  15. log.Printf(&quot;map is %v&quot;, m)
  16. m[&quot;f&quot;](&quot;Hello&quot;)
  17. m[&quot;b&quot;](&quot;World&quot;)
  18. }

答案4

得分: 16

@Seth Hoenig的答案对我帮助最大,但我只想补充一点,Go语言也接受带有定义返回值的函数:

  1. package main
  2. func main() {
  3. m := map[string]func(string) string{
  4. "foo": func(s string) string { return s + "nurf" },
  5. }
  6. m["foo"]("baz") // "baznurf"
  7. }

如果你认为这样写很丑,你也可以使用类型(参见**@smagch**的答案)。

英文:

@Seth Hoenig's answer helped me best, but I just wanted to add that Go accepts functions with defined return value as well:

  1. package main
  2. func main() {
  3. m := map[string]func(string) string{
  4. &quot;foo&quot;: func(s string) string { return s + &quot;nurf&quot; },
  5. }
  6. m[&quot;foo&quot;](&quot;baz&quot;) // &quot;baznurf&quot;
  7. }

If you think it's ugly, you could always use a type (see @smagch's answer).

答案5

得分: 2

我使用了一个map[string]func (a type, b *type),我传递了一个字符串来搜索map,并传递一个指针来修改切片。

希望对你有所帮助!

  1. var Exceptions map[string]func(step string, item *structs.Item)
  2. func SetExceptions() {
  3. Exceptions = map[string]func(a string, i *structs.Item){
  4. "step1": step1,
  5. }
  6. }
  7. func RunExceptions(state string, item *structs.Item) {
  8. method, methBool := Exceptions[state]
  9. if methBool {
  10. method(state, item)
  11. }
  12. }
  13. func step1(step string, item *structs.Item) {
  14. item.Title = "Modified";
  15. }
英文:

I used a *map[string]func (a type, b type) I passed a string to search the map and a pointer to modify the slice.

Hope that helps!

  1. var Exceptions map[string]func(step string, item *structs.Item)
  2. func SetExceptions() {
  3. Exceptions = map[string]func(a string, i *structs.Item){
  4. &quot;step1&quot;: step1,
  5. }
  6. }
  7. func RunExceptions(state string, item *structs.Item) {
  8. method, methBool := Exceptions[state]
  9. if methBool {
  10. method(state, item)
  11. }
  12. }
  13. func step1(step string, item *structs.Item) {
  14. item.Title = &quot;Modified&quot;
  15. }

答案6

得分: 2

希望这对你有用(你可以使用interface{}代替any)

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func toon(v interface{}) {
  6. fmt.Println(v)
  7. }
  8. func main() {
  9. names := map[string]interface{}{
  10. "Function": toon,
  11. }
  12. names["Function"].(func(interface{}))("a")
  13. }
英文:

Hope this works for you(you can use interface{} instead any)

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. func toon(v any) {
  6. fmt.Println(v)
  7. }
  8. func main() {
  9. names := map[string]any{
  10. &quot;Function&quot;: toon,
  11. }
  12. names[&quot;Function&quot;].(func(any))(&quot;a&quot;)
  13. }

答案7

得分: 1

这是我在我的情况下使其工作的方法:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. var routes map[string]func() string
  6. func main() {
  7. routes = map[string]func() string{
  8. "GET /": homePage,
  9. "GET /about": aboutPage,
  10. }
  11. fmt.Println("GET /", pageContent("GET /"))
  12. fmt.Println("GET /about", pageContent("GET /about"))
  13. fmt.Println("GET /unknown", pageContent("GET /unknown"))
  14. // 输出:
  15. // GET / 主页
  16. // GET /about 关于页面
  17. // GET /unknown 404:页面未找到
  18. }
  19. func pageContent(route string) string {
  20. page, ok := routes[route]
  21. if ok {
  22. return page()
  23. } else {
  24. return notFoundPage()
  25. }
  26. }
  27. func homePage() string {
  28. return "主页"
  29. }
  30. func aboutPage() string {
  31. return "关于页面"
  32. }
  33. func notFoundPage() string {
  34. return "404:页面未找到"
  35. }

https://play.golang.org/p/8_g6Di1OKZS

英文:

Here is the way I made it work in my case:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. var routes map[string]func() string
  6. func main() {
  7. routes = map[string]func() string{
  8. &quot;GET /&quot;: homePage,
  9. &quot;GET /about&quot;: aboutPage,
  10. }
  11. fmt.Println(&quot;GET /&quot;, pageContent(&quot;GET /&quot;))
  12. fmt.Println(&quot;GET /about&quot;, pageContent(&quot;GET /about&quot;))
  13. fmt.Println(&quot;GET /unknown&quot;, pageContent(&quot;GET /unknown&quot;))
  14. // Output:
  15. // GET / Home page
  16. // GET /about About page
  17. // GET /unknown 404: Page Not Found
  18. }
  19. func pageContent(route string) string {
  20. page, ok := routes[route]
  21. if ok {
  22. return page()
  23. } else {
  24. return notFoundPage()
  25. }
  26. }
  27. func homePage() string {
  28. return &quot;Home page&quot;
  29. }
  30. func aboutPage() string {
  31. return &quot;About page&quot;
  32. }
  33. func notFoundPage() string {
  34. return &quot;404: Page Not Found&quot;
  35. }

https://play.golang.org/p/8_g6Di1OKZS

huangapple
  • 本文由 发表于 2011年7月21日 05:54:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/6769020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定