在Go语言中,可以使用回调函数和函数作为类型。

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

Using callbacks and functions as a type in go

问题

我正在尝试创建一个类似于Express(NodeJS)路由方法的函数,但是使用Go语言:

app.get("route/here/", func(req, res){
    res.DoStuff()
});

在这个例子中,我希望"foo"(类型)与上述方法中的匿名函数相同。以下是我在Go中尝试的一个失败的方法:

type foo func(string, string)

func bar(route string, io foo) {
    log.Printf("I am inside of bar")
    // 运行io,可能是io()或io(param, param)?
}

func main() {
    bar("Hello", func(arg1, arg2) {
        return arg1 + arg2
    })
}

我应该如何解决这个问题?我是否不应该使用类型,而是使用其他方法?我有哪些选择?

英文:

I am attempting to create a sort of function that is similar to the Express (NodeJS) route method in Go:

app.get("route/here/", func(req, res){
    res.DoStuff()
});    

In this example I want "foo" (the type) to be the same as the anonymous function in the above method. Here is one of my failed attempts using Go:

type foo func(string, string)

func bar(route string, io foo) {
        log.Printf("I am inside of bar")
        // run io, maybe io() or io(param, param)?
}

func main() {
        bar("Hello", func(arg1, arg2) {
                return  arg + arg2
        })
}

How might I fix my dilemma? Should I not use a type and use something else? What are my options?

答案1

得分: 14

你正在走在正确的道路上——在使用函数时为其创建一个类型可以增加更清晰的设计意图和额外的类型安全性。

你只需要稍微修改一下你的示例代码,使其能够编译通过:

package main

import "log"

// 函数的返回类型是其整体类型定义的一部分——将其返回类型指定为字符串,以符合你上面的示例
type foo func(string, string) string

func bar(route string, io foo) {

    log.Printf("I am inside of bar")
    response := io("param", "param")
    log.Println(response)

}

func main() {

    bar("Hello", func(arg1, arg2 string) string {
        return arg1 + arg2
    })

}
英文:

You are on the right track - creating a type for a func in the context you are using it adds clearer design intent and additional type safety.

You just need to modify your example a bit for it to compile:

package main

import "log"

//the return type of the func is part of its overall type definition - specify string as it's return type to comply with example you have above
type foo func(string, string) string

func bar(route string, io foo) {

	log.Printf("I am inside of bar")
	response := io("param", "param")
	log.Println(response)

}

func main() {

	bar("Hello", func(arg1, arg2 string) string {
		return arg1 + arg2
	})

}

答案2

得分: 1

包主

导入 (
"fmt"
"log"
)

类型foo func(string, string)

func bar(route string, callback foo) bool {
//...逻辑

/*你可以返回回调函数来使用参数,同样你也可以验证bar函数*/
callback("param", "param")
return true

}

func main() {
fmt.Println("你好,playground")

res := bar("你好", func(arg1, arg2 string) {
	log.Println(arg1 + "_1")
	log.Println(arg2 + "_2")
})

fmt.Println("bar函数结果:", res)

}

英文:
package main

import (
	"fmt"
	"log"
)

type foo func(string, string)

func bar(route string, callback foo) bool {
 //...logic

  /* you can return the callback to use the 
     parameters and also in the same way you 
     could verify the bar function
   */
	callback("param", "param")
	return true
}

func main() {
	fmt.Println("Hello, playground")

	res := bar("Hello", func(arg1, arg2 string) {
		log.Println(arg1 + "_1")
		log.Println(arg2 + "_2")
	})

	fmt.Println("bar func res: ", res)
}

huangapple
  • 本文由 发表于 2016年10月4日 10:06:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/39843134.html
匿名

发表评论

匿名网友

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

确定