douzhixun8393 2014-09-19 01:00
浏览 45
已采纳

导入和未使用错误

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)
}
  • 写回答

2条回答 默认 最新

  • douhuireng4407 2014-09-19 01:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看