导入但未使用的错误

huangapple go评论84阅读模式
英文:

Import and not used error

问题

我在下面的导入代码中遇到了以下错误:

代码:
package main

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

错误:

.\main.go:9: 导入但未使用: "_/c_/Users/aaaa/IdeaProjects/app/src/api"

有没有什么原因导致导入不起作用?我有一个api包和存储在api文件夹下的文件。

我在main.go中使用以下代码来使用api

func main() {
	// 取消下面的注释以查看包中发生的情况
	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)
}
英文:

I'm getting below error with below import code:

Code:
package main

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

Error:

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

Is there a reason why the import is not working given that I have package api and files stored under api folder?

I'm 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

得分: 7

编译器寻找的是“包的实际使用”,而不是它的存在事实。

你需要使用该包中的某些内容,或者删除导入语句。例如:

v := api.Something ...

如果你在源文件中没有使用该包中的任何内容,那么就不需要导入它。除非你想要运行init函数。在这种情况下,你可以使用忽略符号import _

编辑:

根据你的更新,看起来你在这里覆盖了包的导入:

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

这声明了一个名为api的变量。现在编译器认为它是一个变量,所以你实际上没有使用api包,而是使用了api变量。

你有几个选择。

首先,你可以给那个变量取一个不同的名字(这可能是我会做的):

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

或者,给你的导入起一个别名(虽然这不是我会做的,但也是一个选择):

import (
    // 其他导入语句
    api_package "路径/api"
)

问题在于编译器对于使用哪个内容感到困惑。是使用api包,还是你声明的api变量。

你还应该通过GOPATH导入包,而不是使用相对路径。

英文:

The compiler looks for actual use of a package .. not the fact it exists.

You need to use something from that package.. or remove the import. E.g:

v := api.Something ...

If you don't use anything from that package in your source file .. you don't need to import it. That is, unless you want the init function to run. In which case, you can use the ignore notation import _.

EDIT:

After your update, it appears you're overwriting the package import here:

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

That declares a variable called api. Now the compiler thinks its a variable, and so you're not actually using the api package.. you're using the api variable.

You have a few options.

Firstly, you can call that variable something else (probably what I would do):

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

Or, alias 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 on what to use. The api package.. or the api variable you have declared.

You should also import the package via the GOPATH instead of relatively.

答案2

得分: 1

首先,不要使用相对导入。

如果你的GOPATH包含/Users/aaaa/IdeaProjects/app/src,那么将你的包导入为api

接下来,你正在使用api :=api赋值,这样会导致变量重名。请使用不同的名称。

英文:

First of all, don't use relative imports.

If your GOPATH contains /Users/aaaa/IdeaProjects/app/src, then import your package as api.

Next, you're shadowing api with the assignment to api :=. Use a different name.

答案3

得分: 0

这个错误可能是由于你的.go文件中存在错误,导致编译器无法到达使用它的位置。

英文:

This error can occur if your .go file has an error that is preventing the compiler from reaching the location where it is used.

huangapple
  • 本文由 发表于 2014年9月19日 09:00:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/25924749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定