How to import project specific go packages while maintaining a separate location for go packages that are common to totally different projects?

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

How to import project specific go packages while maintaining a separate location for go packages that are common to totally different projects?

问题

所以我第一次开发Go应用程序。我了解到有两个变量GOROOT和GOPATH用于维护Go包。我现在的理解是,GOROOT用于存放Go二进制文件,而GOPATH主要用于存放项目所需的库和辅助包。

这是我的当前项目结构 -

/Users/john/work/project-mars
/Users/john/work/project-mars/main.go
/Users/john/work/project-mars/helper
/Users/john/work/project-mars/helper/helper.go

main.go的内容

package main

import (
    "fmt"
    "helper"
)

func main(){
    fmt.Println("Hello")
}

helper.go的内容

package helper

import (
    "fmt"
)

func SayWorld(){
    fmt.Println("World")
}

Go变量如下 -

GOROOT = /Users/john/apps/go
GOPATH = /Users/john/apps/go-packages

问题:
现在,当我执行以下命令时,我得到了这个错误 -

mac-machine:project-mars john$ go build main.go 
main.go:5:5: cannot find package "helper" in any of:
    /Users/john/apps/go/src/helper (from $GOROOT)
    /Users/john/apps/go-packages/src/helper (from $GOPATH)

我理解GOPATH应该是我正在工作的项目目录。但我担心以一种模块化的方式保存我的项目和库包,这样当我以后有一个完全不同的项目(即project-aurora),它可能使用相同的GitHub辅助包时,它们不会在project-mars和project-aurora中下载两次。

在处理不同项目时,我如何避免这种冗余?

更新:并不是说我不能编译它们。我可以将GOPATH设置为我的项目目录,并使用src、pkg、bin项目布局,重新组织文件,最终编译项目。太棒了。但我的问题是如何解决在这种单一GOPATH方式下出现的常见包冗余问题。

英文:

So I was developing a go application for the very first time. I came to know that there are two variables GOROOT and GOPATH which are used to maintain go packages. What I understand till now, is that GOROOT is for the go binary files and GOPATH is mainly for storing library and helper packages that is needed for projects.

Here is my current project structure -

/Users/john/work/project-mars
/Users/john/work/project-mars/main.go
/Users/john/work/project-mars/helper
/Users/john/work/project-mars/helper/helper.go

Content of main.go

package main

import (
    "fmt"
    "helper"
)

func main(){
    fmt.Println("Hello")
}

Content of helper.go

package helper

import (
    "fmt"
)

func SayWorld(){
    fmt.Println("World")
}

And the go variables are -

GOROOT = /Users/john/apps/go
GOPATH = /Users/john/apps/go-packages

Problem:
Now when I perform the following command, I get this error -

mac-machine:project-mars john$ go build main.go 
main.go:5:5: cannot find package "helper" in any of:
	/Users/john/apps/go/src/helper (from $GOROOT)
	/Users/john/apps/go-packages/src/helper (from $GOPATH)

I understand that GOPATH should be the project directory that I am working on. But I am concerned with keeping my projects and library packages in a modular way, so that when later I have a totally different project (i.e. project-aurora) which might use same github helper packages, that they are not downloaded two times, both in project-mars and project-aurora .

How can I avoid this redundancy while working on different projects ?

Update: It's not that I can not compile them. I can use the GOPATH as my project directory and use src,pkg,bin project layouts and also reorganize the files and finally get to compile the project. yeeeeppi. But my question is about resolving the redundancy of common package problem that appears in this single GOPATH way.

答案1

得分: 1

请仔细阅读如何编写Go代码。它解释了你需要了解的一切。

你不应该将GOPATH作为你的项目目录。假设你想使用标准的Go工具,你的包源代码应该位于与其导入路径对应的目录中,就像其他任何包一样。

你的项目应该位于$GOPATH/src/project-mars,可以通过go install project-mars进行构建。helper包应该位于$GOPATH/src/project-mars/helper,并通过"project-mars/helper"进行导入。

英文:

Please read How to Write Go Code carefully. It explains everything you need to know.

You don't use GOPATH as your project directory. Assuming you want to work with the standard Go tooling, your package source needs to be in the directory corresponding to its import path, just like any other package.

Your project should be located at $GOPATH/src/project-mars, which can be built via go install project-mars. The helper package should be located at $GOPATH/src/project-mars/helper, and imported via "project-mars/helper".

答案2

得分: 0

将你的helper-lib文件夹重命名为helper

然后将该文件夹从project-mars移动到上层文件夹work

这样应该可以使你在main.go中的

import "helper"

语句正常工作。

英文:

Rename your helper-lib folder to helper

Then move this folder from project-mars to the upper folder work

This should make your

import "helper" 

statement in main.go work.

huangapple
  • 本文由 发表于 2016年3月15日 06:25:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/35999277.html
匿名

发表评论

匿名网友

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

确定