英文:
Using function as parameter for if statement in go
问题
我正在尝试让以下代码工作:
func ArticlesHandler(w http.ResponseWriter, r *http.Request) {
...
if(ValidTokenProvided(w,r)){
...
}
...
}
func ValidTokenProvided(w http.ResponseWriter, r *http.Request) {
...
return false
}
我如何将一个函数作为参数传递?如果不能,那么实现这个功能的最佳方法是什么?
英文:
I'm trying to get the following to work:
func ArticlesHandler(w http.ResponseWriter, r *http.Request) {
...
if(ValidTokenProvided(w,r)){
...
}
...
}
func ValidTokenProvided(w http.ResponseWriter, r *http.Request) {
...
return false
}
How can I pass a function as a parameter? or if I can't, what is the best way to achieve this.
答案1
得分: 2
func ValidTokenProvided(w http.ResponseWriter, r *http.Request) bool {
...
英文:
ValidTokenProvided was a void method.. Made it bool:
func ValidTokenProvided(w http.ResponseWriter, r *http.Request) bool {
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论