Golang mod cannot use func literal (type func(*"github.com/dgrijalva/jwt-go".Token)

  docker, go

My code works ok, until i tryed to wrap it in docker container.

I run my code with just two commands

go build .
go run main.go

To store dependencies for docker i used

go mod init
go build

When go build is running, im getting error.

./main.go:19:2: cannot use func literal (type func(*"github.com/dgrijalva/jwt-go".Token) (interface {}, error)) as type "github.com/form3tech-oss/jwt-go".Keyfunc in field value

I read something about vendor folder, but i dont have it in my project. My project structure:
enter image description here

I cant understand why i getting error. As i read, there could be mistakes in my imports after go mod, but i cannot find any examples of what should i change.

My main.go file:

package main

import (
    "fmt"
    "go_contacts/controllers"
    _ "go_contacts/docs"
    "net/http"
    "os"

    jwtmiddleware "github.com/auth0/go-jwt-middleware"
    "github.com/dgrijalva/jwt-go"
    "github.com/gorilla/mux"
    "github.com/joho/godotenv"

    httpSwagger "github.com/swaggo/http-swagger"
)

var jwtMiddleware = jwtmiddleware.New(jwtmiddleware.Options{
    ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
        mySigningKey := os.Getenv("JWT_TOKEN_KEY")
        return []byte(mySigningKey), nil
    },
    SigningMethod: jwt.SigningMethodHS256,
})

// @title Test Golang API
// @version 1.0
// @description Swagger API for Golang Project.

// @contact.email [email protected]
// @BasePath /

func main() {
    godotenv.Load(".env")

    router := mux.NewRouter()

    router.PathPrefix("/swagger/").Handler(httpSwagger.Handler(
        httpSwagger.URL("http://localhost:7000/swagger/doc.json"),
        httpSwagger.DeepLinking(true),
        httpSwagger.DocExpansion("none"),
        httpSwagger.DomID("#swagger-ui"),
    ))

    router.HandleFunc("/api/user/create", controllers.CreateUserResp).Methods("POST")
    router.HandleFunc("/api/login", controllers.LoginResp).Methods("POST")
    router.Handle("/api/user/{id}", jwtMiddleware.Handler(http.HandlerFunc(controllers.GetUserResp))).Methods("GET")
    router.Handle("/api/user", jwtMiddleware.Handler(http.HandlerFunc(controllers.GetCurrentUserResp))).Methods("GET")

    port := os.Getenv("PORT")
    if port == "" {
        port = "8000"
    }

    err := http.ListenAndServe(":"+port, router)
    if err != nil {
        fmt.Print(err)
    }
}

Source: Docker Questions

LEAVE A COMMENT