-1

I have go code which looks like this

package services

import (
    "net/http"
    "strings"

    "github.com/gin-gonic/gin"
    "github.com/irohitb/EmpAdmin/backend/config"
    "github.com/irohitb/EmpAdmin/backend/middleware"
    "github.com/supabase/postgrest-go"
)


func GetAllUsers(env *config.Env, db *postgrest.Client, group *gin.RouterGroup) {
    group.GET("/:services", func (router *gin.Context) {
        services := strings.Split(router.Param("service"), ",")
        user, exists := router.Get("user")
        if !exists {
            router.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "user information not found in context"})
            return
        }
        userToken, exists := user.(*middleware.UserToken)
        if exists {
            workspaceId := userToken.User.WorkspaceID
        }

        
        
        
    })
}

Here VS code complains about

    "github.com/irohitb/EmpAdmin/backend/middleware"

imported but not used error.

which I am using here

*middleware.UserToken

Here is how my middleware looks

package middleware

import (
    "net/http"

    "github.com/dgrijalva/jwt-go"
    "github.com/gin-gonic/gin"
    "github.com/irohitb/EmpAdmin/backend/domain"
)




type UserToken struct {
    jwt.StandardClaims
    User domain.UserToken
}
Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
  • 1
    Do you get an error when running `go build`? (if so edit your question and add details). – Brits Apr 30 '23 at 10:09
  • @Brits no error when I do `go run main.go` – Alwaysblue May 01 '23 at 08:03
  • Have you tried ```Ctrl + Shift + p ``` > Developer: Reload window. Sometimes VS code fails to index the code properly. See this answer: [https://stackoverflow.com/questions/71023129/module-lookup-disabled-by-goproxy-off-but-go-env-shows-goproxy-is-set/73049786#73049786] – ABDULLOKH MUKHAMMADJONOV May 02 '23 at 15:44
  • @ABDULLOKHMUKHAMMADJONOV Yap – Alwaysblue May 02 '23 at 19:54
  • Note: The `workspaceId` variable is defined inside an `if` block, which means it won't be accessible outside the block. To fix this, declare the variable before the `if` statement. And use `=`, not `:=` for the assignment. – VonC May 03 '23 at 07:13
  • please see this if it helps: https://stackoverflow.com/questions/25924749/import-and-not-used-error – blackgreen May 10 '23 at 06:25

2 Answers2

0

The repository github.com/irohitb/EmpAdmin seems to be a private one, which could have that kind of side effect.

Try and add in your VSCode preferences "Go tools env vars":

{
  "go.toolsEnvVars": {
    "GOPRIVATE": "github.com/*"
  }
}

Also, as in this answer, check if you have defined elsewhere a variable (or global variable) named middleware, which would override the package middleware.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Wouldn't `github.com/*` apply for every project on GitHub? Surely, one would want to scope it to just the username or repository in question. – Tyler Kropp May 05 '23 at 15:10
  • 1
    @TylerKropp I agree. I was suggesting that as a test. If it works, then we can test a more previse value. – VonC May 05 '23 at 15:20
-2

My argument is in favor of VS Code check that we are not "using" the middleware import.

By using, it most probably means "Actually using something from that package". e.g., the UserToken struct. Here we are not actually using it, we are just assigning its type to variables which in turns are unused. Apparently, we're using middleware.UserToken, but not really, nowhere in that function are we are creating a UserToken struct.

userToken, exists := user.(*middleware.UserToken) 
// Use userToken here
if exists {
      workspaceId := userToken.User.WorkspaceID
}

Merely assigning a type to a variable is not "using" that type. To fix the warning, we need to actually use something from the middleware package: construct a UserToken struct instance.

// This is an actual usage
userToken := middleware.UserToken{User: domain.UserToken{WorkspaceID: workspaceId}}

And now the warning will go away, because we're actually using middleware.UserToken.

Jishan Shaikh
  • 1,572
  • 2
  • 13
  • 31
  • The meaning of "using" something from a package is not a matter of opinion. It's a behavior mandated by the [language specification](https://go.dev/ref/spec#Import_declarations): *It is illegal for a package [...] to directly import a package without referring to any of its exported identifiers.* – blackgreen May 10 '23 at 06:33