'Mixed named and unnamed function parameters
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.
Solution 1:[1]
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)
Solution 2:[2]
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
.
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | matt.s |
Solution 2 | Stephen Rauch |
Solution 3 | Aniket Kariya |