英文:
Golang - how to define multiple projects in workspace
问题
在Go语言中,GOPATH指向工作区。我可以在我的工作区创建多个项目,并且让GOPATH指向这些项目的位置列表吗?
英文:
The GOPATH in Go points to the workspace. Can I create multiple projects in my workspace and have GOPATH point to a list of the locations of these projects?
答案1
得分: 2
是的,您可以在工作区中拥有多个项目。但是,您不需要为此指定多个GOPATH
。您只需在该GOPATH
环境中创建您的两个项目。要编译、运行等,您只需指定您想要使用的入口点。
例如:
go run src/proj1/proj1.go
go run src/proj2/proj2.go
有关GOPATH
和工作区的更多信息,请参阅工作区的 godoc。
具体来说,“src目录包含按包组织的Go源文件(每个目录一个包)”。请注意,您不仅限于一个主包。
英文:
Yes you can have multiple projects in your workspace. However, you do not specify multiple GOPATH
s for that. You simply create your two projects within that GOPATH
environment. And to compile, run etc you simply specify the entry point you want to use.
E.g.
go run src/proj1/proj1.go
go run src/proj2/proj2.go
For more information on GOPATH and workspaces, see the godoc on workspaces.
Specifically, “src contains Go source files organized into packages (one package per directory),”. Notice that you are not limited to only one main package.
答案2
得分: 0
你可以使用单个工作区,但如果你想在工作区之外使用另一个项目,你需要检查你的导入。因为当你导入golang包时,
import "fmt"
它会在GOROOT中搜索"fmt"包
或者通过
go get github.com/package
获取其他包,它会将包放在%workspace(GOPATH)%\src\github.com
下。它不会将包放在你的项目下。所以你可以将第三方项目克隆到你的项目文件夹下,并使用相对路径表示法设置导入:
import "./github.com/package"
然后运行你的go文件。它会起作用。
英文:
You can use single workspace but if you want to work with another project out of workspace, you should check your imports. Because when you import golang packages
import "fmt"
It searchs "fmt" package on GOROOT
or other packages which is get via
go get github.com/package
It puts package under %workspace(GOPATH)%\src\github.com
. It doesn't put package under your project. So you can clone 3rd party projects under your project folder and set imports like relative path notation:
import "./github.com/package"
then run your go files. It works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论