Import and unused error

I am getting below error with below import code:

code: main package

import (
    "log"
    "net/http"
    "os"
    "github.com/emicklei/go-restful"
    "github.com/emicklei/go-restful/swagger"
    "./api"
)

      

Mistake:

.\main.go:9: imported and not used: "_/c_/Users/aaaa/IdeaProjects/app/src/api"

      

Is there a reason why the import isn't working, given that I have the package api

files stored in the api folder too?

I am using below to use api

in main.go

func main() {
    // to see what happens in the package, uncomment the following
    restful.TraceLogger(log.New(os.Stdout, "[restful] ", log.LstdFlags|log.Lshortfile))

    wsContainer := restful.NewContainer()
    api := ApiResource{map[string]OxiResp{}}
    api.registerLogin(wsContainer)
    api.registerAccount(wsContainer)
    api.registerLostLogin(wsContainer)
    api.registerWallet(wsContainer)
}

      

+1


source to share


2 answers


The compiler looks for the actual usage of the package. Not the fact that it exists.

You need to use something from this package .. or remove the import. For example:

v := api.Something ...

      

If you don't use any of this package in your source file, you don't need to import it. That is, if you don't want the function to run init

. In this case, you can use the ignore notation import _

.

EDIT:

After your update, it looks like you are rewriting the package import here:

api := ApiResource{map[string]OxiResp{}}

      

Declares a variable named api

. Now the compiler is reading its variable and so you are not actually using the package api

.. you are using the variable api

.



You have several options.

First, you can call this variable something else (perhaps what I will do):

apiv := ApiResource{map[string]OxiResp{}}

      

Or for example your import (not what I would do ... but an option nonetheless):

import (
    // others here
    api_package "./api"
)

      

The problem is that the compiler is confused about which to use. Package api

.. or the variable api

you specified.

You must also import the package via GOPATH

instead of relative.

+4


source


First of all, don't use relative imports.

If yours GOPATH

does /Users/aaaa/IdeaProjects/app/src

, then import your package as api

.



Then you shade the api with assignment api :=

. Please use a different name.

+1


source







All Articles