英文:
Using local packages with gb
问题
我正在开始一个新项目,并考虑使用gb
作为我的构建工具,但它似乎与vscode
集成得不太好...
我使用gb vendor fetch
没有问题地引用了第三方依赖项,但是创建本地包却有点棘手!我是否漏掉了一些明显的东西?
这是我的本地src目录:
src
/cmd
/model
calc.go
/server
server.go
以下代码成功编译并创建了一个bin\server.exe
文件,但是导入路径没有被识别,gocode
也无法识别它。
这是服务器代码:
package main
import (
"cmd/model" // 不是一个正确的引用...
"fmt"
)
func main() {
fmt.Println(model.Add(1, 2))
}
这是模型代码:
package model
func Add(a int, b int) int {
return a + b
}
我在Github上找到了一个类似的问题(https://github.com/joefitzgerald/go-plus/issues/325),尽管nsf的解决方案解决了自动完成的问题(导入后),但导入语句本身仍然声称在GOROOT和GOPATH中搜索。
有什么想法吗?
英文:
I'm starting a new project and considering gb
as my build tool but it doesn't appear to be integrating very well with vscode
...
I've referenced 3rd party dependencies no problem using gb vendor fetch
but as for creating local packages, this is proving a little trickier! Am I missing something obvious?
Here's my local src directory:
src
/cmd
/model
calc.go
/server
server.go
The following code compiles and creates a bin\server.exe
file successfully but the import path isn't picked up, nor does gocode
recognise it
Here's the server code:
package main
import (
"cmd/model" // not a happy reference...
"fmt"
)
func main() {
fmt.Println(model.Add(1, 2))
}
Here's the model code:
package model
func Add(a int, b int) int {
return a + b
}
I've found what appears to be a similar issue on Github (https://github.com/joefitzgerald/go-plus/issues/325) and while nsf's solution sorts out auto-complete (post import), the import statement itself still claims to be searching in the GOROOT and GOPATHs.
Any ideas?
答案1
得分: 1
感谢lukehoban在这里的回答https://github.com/Microsoft/vscode-go/issues/249,我成功地使我的环境工作起来了。
我只是在.vscode目录下创建了一个settings.json文件(现在必须要检查进去),并在其中进行了配置:
{
"go.gopath": "${workspaceRoot}"
}
这让我感觉不太好,而且它仍然没有提供一种同时引用第三方依赖和本地包的方法...
英文:
Thanks to an answer from lukehoban here https://github.com/Microsoft/vscode-go/issues/249 I was able to get my environment working.
I simply created a settings.json file under the .vscode directory (which will now have to be checked in) into which I've configured:
{
"go.gopath": "${workspaceRoot}"
}
This makes me feel unclean and it still doesn't provide a way to reference both 3rd party dependencies and local packages together...
答案2
得分: 0
不要试图对抗Go,要与Go合作。
首先,给所有的包提供完全限定的导入路径。Go是围绕全局导入路径设计的,不要试图强制Go使用扁平的层次结构甚至相对路径。
你可以直接指向导入路径仓库的端点,也可以使用Go的远程导入路径机制。顺便说一句,如果你运行的是自托管的GitLab实例,它支持开箱即用的远程导入路径元标签。
我更喜欢glide,但也许以下内容在gb中也是可能的。当然,即将推出的go dep也会有类似的功能:你可以使用glide的repo stanza指向ssh+git端点和其他端点。坦率地说,我不知道gb是否支持等效的机制,但如果它不支持,这是重新考虑的一个很好的理由。
英文:
Do not try to work against Go, work with Go.
First of all give all your packages fully qualified import paths. Go is designed around global import paths, do not try to force Go into using flat hierarchies or even relative paths.
You can point to your import path repository endpoints either directly or by using Go's remote import path mechanism. BTW, if you happen to run a self-hosted GitLab instance, it supports remote import path meta tags out of the box.
I prefer glide, but maybe the following is possible with gb, too. Certainly something simililar will be possible with the upcoming go dep: You can point to ssh+git endpoints and others using glide's repo stanza. Frankly I have no idea if gb supports an equivalent mechanism, but if it doesn't this is a good reason to reconsider.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论