douzhao1912 2016-04-05 17:55
浏览 42
已采纳

转到自定义包导入错误

I am trying to write a program where I have a structure like:

Go/src/
  -github.com
     -myname
        -hello
           main.go
           -vector
              vector.go

When I import the package in my code inside of the main.go file using a command like:

import(
  "vector"
)

I get the error message:

Can't find package "vector" in any of:
    C:\Go\src\vendor\vector (vendor tree)
    C:\Go\src\vertex (from $GOROOT)
    C:\Go\src\github.com\myname\src\vertex (from $GOPATH)

Why is it adding src on that last line? Shouldn't it replace the src with the hello folder since that is where I'm running the file from? Also, it runs if I import it from the full file structure like github.com/myname/hello/vertex which seems strange to me.

I am executing using go run hello.go to simplify my interaction with the program.

  • 写回答

1条回答 默认 最新

  • duanfen2008 2016-04-05 18:07
    关注

    Your Go code (as opposed to Go's stdlib) is meant to be under $GOPATH/src (edit: not $GOROOT, as I initially said!), and it's standard to always use the full import path, in your case starting with github.com/ (even if you figured out a way to avoid having to).

    There is more in How to Write Go Code by the Go team, and other answers here describing project layout and the first steps to setting a workspace up.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64