【发布时间】:2019-08-09 06:42:38
【问题描述】:
我是新手,正在尝试使用 gocenter 远程 repo (Artifactory 6.8) 来解决依赖关系。尽管设置了 GOPROXY env var,但我的 gocenter-cache 存储库仍然为空。
这是我的代码。
package main
import (
"fmt"
"github.com/naoina/go-stringutil"
)
func main() {
var str string = "hello_world_go"
fmt.Println(stringutil.ToUpperCamelCase(str)) //prints HelloWorldGo
}
我要解决的依赖关系在这里:https://search.gocenter.io/github.com~2Fnaoina~2Fgo-stringutil/versions
这是我的 GOPROXY 环境变量:
$ echo $GOPROXY
https://<<my Artifactory URL>>/api/go/gocenter
这是 gocenter 存储库定义的截断版本。我使用jfrog docs 进行设置:
{
"key" : "gocenter",
"packageType" : "go",
"url" : "https://gocenter.io/",
"rclass" : "remote"
}
当我运行“go get”时,依赖解决了......
$ go get -v github.com/naoina/go-stringutil
github.com/naoina/go-stringutil (download)
但是 gocenter-cache 是空的,告诉我它没有被使用。
{
"repo" : "gocenter-cache",
"path" : "/",
"created" : "2019-04-17T16:35:37.586Z",
"lastModified" : "2019-04-17T16:35:37.586Z",
"lastUpdated" : "2019-04-17T16:35:37.586Z",
"children" : [ ],
"uri" : "https://<<REDACTED>>/api/storage/gocenter-cache"
}
也许我的“go get”应该有不同的目标?我只是在使用 gocenter 中存在的内容:https://search.gocenter.io/github.com~2Fnaoina~2Fgo-stringutil/versions
任何帮助指出我做错了什么将不胜感激。我还没有进入模块或类似的东西,因为我是一个工件管理员而不是一个 go 开发人员。我只是在尝试对这个功能进行原型设计,以便我们可以帮助建议我们的用户做什么。
--更新:我找到了https://golang.org/cmd/go/#hdr-Module_proxy_protocol,然后尝试了这个:
$ echo $GOPROXY; go get $GOPROXY/github.com/naoina/go-stringutil
https://<<REDACTED>>/api/go/gocenter
package https:/<<REDACTED>>/api/go/gocenter/github.com/naoina/go-stringutil: https:/<<REDACTED>>/api/go/gocenter/github.com/naoina/go-stringutil: invalid import path: malformed import path "https:/<<REDACTED>>/api/go/gocenter/github.com/naoina/go-stringutil": invalid char ':'
所以我的协议和我的网址之间的冒号是无效字符?另外为什么要删除我的一个正斜杠?
--更新2: 我得到了“go mod init”的工作,有点:
$ go mod init
go: creating new go.mod: module example/hello
$ ls
go.mod hello.go
$ go build
build example/hello: cannot load github.com/naoina/go-stringutil: cannot find module providing package github.com/naoina/go-stringutil
$ cat go.mod
module example/hello
go 1.12
$ echo $GOPROXY
https://<<REDACTED>>/api/go/gocenter
--更新 3:
$ cat go.mod
module example/hello
go 1.12
require github.com/naoina/go-stringutil v0.1.0
$ go build
hello.go:6:2: cannot find package "github.com/naoina/go-stringutil" in any of:
C:\Go\src\github.com\naoina\go-stringutil (from $GOROOT)
C:\Users\samuelm\go\src\github.com\naoina\go-stringutil (from $GOPATH)
$ echo $GOPROXY
https://<<REDACTED>>/api/go/gocenter
--更新 4:看来我还没有使用模块?
$ go list -m all
go list -m: not using modules
--更新 5,回复:retgits 你的步骤有所帮助,但我仍然没有一路走来。
$ find .
.
./bin
./src
./src/example.com
./src/example.com/hello
./src/example.com/hello/hello.go
$ cd src/
$ go mod init example.com/hello
go: creating new go.mod: module example.com/hello
$ cat go.mod
module example.com/hello
go 1.12
$ go get -v github.com/naoina/go-stringutil
Fetching https://<<REDACTED>>/api/go/gocenter/github.com/naoina/go-stringutil/@v/list
Fetching https://<<REDACTED>>/api/go/gocenter/github.com/naoina/@v/list
Fetching https://<<REDACTED>>/api/go/gocenter/github.com/@v/list
go get github.com/naoina/go-stringutil: unexpected status (https://<<REDACTED>>/api/go/gocenter/github.com/naoina/go-stringutil/@v/list): 404 Not Found
$ go build example.com/hello
can't load package: package example.com/hello: unknown import path "example.com/hello": cannot find module providing package example.com/hello
$ cd example.com/hello/
$ go build
build example.com/hello/example.com/hello: cannot load github.com/naoina/go-stringutil: cannot find module providing package github.com/naoina/go-stringutil
我没有在我的 GOPROXY 中提供凭据,因为我们的最终用户没有帐户,我们处于防火墙环境中并允许完全匿名读取访问。如果我们必须提供用户帐户,那么我们将无法支持 Go。
--最终更新: 删除我的本地代理解决了 404 问题,retgits 解决方案有效。
【问题讨论】:
-
您是否在模块感知的 Go 项目中运行命令?换句话说,您在其中运行命令的文件夹是否有
go.mod文件?比较我的机器在 Go 项目中使用 Go mod 和空文件夹(不是 Go 项目)运行它的结果,我看到的结果和你一样(这是 Go 客户端的预期行为) -
要使用 go mod,您可以执行以下操作:
echo "module test" > go.mod; go mod tidy。如果您的代理设置正确,它应该可以工作。 -
我没有 go.mod 文件。我不知道这是否需要。我会试试的。
-
我得到:“go: GO111MODULE=auto 在 GOPATH/src 中禁用了模块;请参阅‘go help modules’”。我没有设置环境变量“GO11MODULE”。
-
查看 update2 以创建我的 go.mod 文件。
标签: go artifactory