英文:
Mixed named and unnamed function parameters
问题
我有一个用于验证JWT令牌的函数(不是中间件),代码如下:
package main
import (
"net/http"
"log"
"fmt"
"github.com/dgrijalva/jwt-go"
)
func ValidateToken(w http.ResponseWriter, r *http.Request) *jwt.Token {
// 解析令牌
token, err := jwt.ParseFromRequest(r, func(token *jwt.Token) (interface{}, error) {
return VerifyKey, nil
})
// 验证令牌
if err != nil {
switch err.(type) {
// 验证过程中出错
case *jwt.ValidationError:
vErr := err.(*jwt.ValidationError)
switch vErr.Errors {
case jwt.ValidationErrorExpired:
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintln(w, "Token expired")
return nil
default:
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, "Error parsing token")
log.Printf("Validation error: %v\n", err)
return nil
}
// 其他错误
default:
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, "Error parsing token")
log.Printf("Validation error: %v\n", err)
return nil
}
}
return token
}
然后,在我的处理程序中,我调用这个函数来获取令牌,并使用JWT Token结构体中的Valid属性检查它是否有效。然而,当我运行Web服务器时,我在第13行遇到一个错误,错误信息是:
Mixed named and unnamed function parameters
第13行是jwt.ParseFromRequest()的调用。你对我做错了什么有什么想法吗?我对Go语言还不熟悉。
英文:
I have this function for authenticating JWT tokens (not middleware), which says:
package main
import (
"net/http"
"log"
"fmt"
"github.com/dgrijalva/jwt-go"
)
func ValidateToken(w http.ResponseWriter, r *http.Request) *jwt.Token {
//parse token
token, err := jwt.ParseFromRequest(r, func(token *jwt.Token) (interface{}, err error) {
return VerifyKey, nil
})
//validate token
if err != nil {
switch err.(type) {
//something went wrong during validation
case *jwt.ValidationError:
vErr := err.(*jwt.ValidationError)
switch vErr.Errors {
case jwt.ValidationErrorExpired:
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintln(w, "Token expired")
return nil
default:
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, "Error parsing token")
log.Printf("Validation error: %v\n", err)
return nil
}
//something else went wrong
default:
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, "Error parsing token")
log.Printf("Validation error: %v\n", err)
return nil
}
}
return token
}
Then, in my handlers, I call this function to get the token and check if it is valid using the Valid property in the JWT Token struct. However, when I run the web server, I get an error on line 13, saying:
Mixed named and unnamed function parameters
Line 13 is the jwt.ParseFromRequest() call. Any thoughts on what I am doing wrong? I am new to Go.
答案1
得分: 9
token, err := jwt.ParseFromRequest(r, func(token *jwt.Token) (interface{}, err error)
你的函数在这里返回两个参数,一个是接口类型,一个是错误类型。
你需要给它们都起一个名字,像这样 (x interface{}, err error)
或者
你两个都不起名字,像这样 (interface{}, error)
因为,
你现在的参数列表是两个错误类型的参数。
就像你说的:
var x, y, z int
你有一个 int 类型的列表,你写的是你有两个以逗号分隔的错误类型的变量,名字分别是 interface{}
和 err
。编译器知道 interface{}
是一个愚蠢的错误变量名,它指出你需要一个变量来表示第一个(它认为是真正的)类型,或者你应该去掉 err
这个词。
英文:
token, err := jwt.ParseFromRequest(r, func(token *jwt.Token) (interface{}, err error)
Your function here returns two parameters, an interface, and and error.
You need to name BOTH like ( x interface{}, err error )
or
You need to name NEITHER like ( interface{}, error )
because,
What you have there right now is a parameter list of two error type parameters.
Like when you say:
var x, y, z int
you have a list of ints, you wrote that you have 2 comma-separated variables of type error named interface{}
and err
. The compiler knows interface{}
is a stupid name for an error variable, and is pointing out that you need a variable for the first (what it presumes is really a) type, or you should ditch the word err
.
答案2
得分: 1
你正在定义一个内联函数(类型为jwt.Keyfunc
),但没有将其绑定到任何东西上。如果VerifyKey
的类型是jwt.Keyfunc
,那么你可以将第13行改为:
token, err := jwt.ParseFromRequest(r, VerifyKey)
英文:
You are defining a function inline (of type jwt.Keyfunc
), but not binding it to anything. If VerifyKey
is of type jwt.Keyfunc, then you can just change line 13 to
token, err := jwt.ParseFromRequest(r, VerifyKey)
答案3
得分: 0
同样的错误也可能发生在参数列表中忘记了逗号,
的情况下。
func someFunc(a string, b string c int) {} // 在 string 和 c 之间
英文:
the same error can also occur if you forgot ,
in between the arguments list.
func someFunc(a string, b string c int) {} // between string and c
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论