电线波
电线波

Reputation: 21

Questions about golang importing jwt

package common

import (
     "github.com/dgrijalva/jwt-go"
)

type Claims struct {
   UserId uint
   jwt.StandardClaims
}

Above is my code. Today, after using the gin and gorm framework normally, I created a new jwt.go file in the common package. When I run the main function, golang prompts an error (undefined: jwt) in the last line, but in the go.mod file, I am prompted that I have downloaded "github. com/dgrijalva/jwt-go".

I tried to get github.com/dgrijalva/jwt-go and import go get - u github.com/golang-jwt/jwt again, but neither of the two methods worked

Upvotes: 1

Views: 720

Answers (2)

Jim Naumann
Jim Naumann

Reputation: 187

I just ran a test of this, and it compiled and ran.

> go mod init scratch
> go get github.com/golang-jwt/jwt 

Then in scratch.go:

package main 

import (
     "fmt"
     "github.com/golang-jwt/jwt"
)

type Claims struct {
   UserId uint
   jwt.StandardClaims
}

func main() {
  var deez Claims
  fmt.Println(deez.UserId)
}

Then :

❯ go run ./scratch.go
0

Maybe you need to remove github.com/dgrijalva/jwt-go from go.mod with go mod tidy?

Upvotes: 0

Sanan Khan
Sanan Khan

Reputation: 1

import (
    "github.com/dgrijalva/jwt-go"
)

then write in the terminal go mod tidy it will work

Upvotes: 0

Related Questions