导入和未使用错误 [英] Import and not used error

查看:51
本文介绍了导入和未使用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下导入代码,我遇到了以下错误:

I'm getting below error with below import code:

代码:包主

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

错误:

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

鉴于我有 package api 和存储在api文件夹下的文件,是否有导入为什么不起作用的原因?

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

我正在下面使用main.go

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)
}

推荐答案

编译器会寻找包的实际使用方式 ..而不是它存在的事实.

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 ...

如果您在源文件中不使用该包中的任何内容,则无需导入.也就是说,除非您要运行 init 函数.在这种情况下,可以使用忽略符号 import _ .

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 _.

更新后,您似乎在这里覆盖了包导入:

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

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

它声明了一个名为 api 的变量.现在,编译器认为它是一个变量,因此您实际上并没有使用 api 包.而是使用了 api 变量.

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.

您有一些选择.

首先,您可以将该变量称为其他名称(可能是我会做的事情):

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"
)

问题是编译器对使用什么感到困惑. api 包..或您声明的 api 变量.

The problem is that the compiler is confused on what to use. The api package.. or the api variable you have declared.

您还应该通过 GOPATH 而不是相对地导入软件包.

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

这篇关于导入和未使用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆