doulutian4843 2018-09-10 08:44
浏览 226

即使文件存在,通过github.com repo进行Golang本地导入也不起作用

I do understand Go is quirky for imports, but I've tried following the convention (I believe) to the point, however I cannot import a struct.

Project structure:

/project-name
    /parser/main.go
    /query/main.go
... Project files in root

I have a struct in /parser/main.go exported:

package parser
type SomeTranslationStuff struct {
    ID                  int    `json:"Id"`
    Language            string `json:"Language"`
}

I wish to import this in /query/main.go.

I do it as such:

import (
    "github.com/org/project-name/parser"
)

It does not import it - I am met with "cannot find package ... in Gopath".

My project exists under: Users\user\go\src\project-name. The imported project(which is the same project, imported from github via go get) does indeed exist in Users\user\go\src\github.com\org\project-name.. Everything seems according to the "Go"-way of importing stuff, yet it doesn't appear to import?

It specifically says:

Cannot find package name:
C:\Users\user\go\src\github.com\org\project-name\parser(from $GOPATH)

If I go to that path, the project is, correctly, there! What gives? Why doesn't it import?

  • 写回答

1条回答 默认 最新

  • doufu8887 2018-09-10 09:04
    关注

    A package has two things:

    1. A name. This is set by the package thename declaration.
    2. An import path. This path is relative to $GOPATH/src.

    You should (no arguing here) do the following:

    1. The name and the last component of the import path should match. E.g. if the import path is "sun/moon/stars" than the package name is "stars". (A lot of people get this wrong and either call the package or the folder stuff like go-stars. Don't do that.)

    2. Your folder structure on disk must match the import paths of your package. If the import path of your package is "whatever/random/noise" you must place it under $GOPATH/src/whatever/random/noise. Or put the other way around: If you code is in $GOPATH/src/ill/do/it/my/own/way then the import path of package "way" is "ill/do/it/my/own/way".

    3. If you want you package to be go getable place it on a known code hosting and import it by the full import path. Restrictions on import path might apply. Place code of package "foo" under $GOPATH/src/github.com/you/repo/whatever/deep/folder/structure/foo and import it with import "github.com/you/repo/whatever/deep/folder/structure/foo"

    4. All your paths and package names should be all lowercase.

    It boils down to: Do not do anything fancy, keep it natural. Import path and folder structure must match.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题