如何使用 gocenter remote Artifactory repo 进行本地依赖解析?
How to use gocenter remote Artifactory repo for local dependency resolution?
我是新手,正在尝试使用 gocenter 远程存储库 (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 回购定义的 t运行 版本。我使用 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
如果能帮助指出我做错了什么,我们将不胜感激。我还没有进入模块或类似的东西,因为我是一个 artifactory 管理员而不是开发人员。我只是在尝试制作此功能的原型,以便我们可以帮助建议用户如何操作。
--更新:我找到了 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 ':'
所以我的协议和 url 之间的冒号是无效字符?另外为什么要删除我的一个正斜杠?
--更新 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 开发人员,所以让我向您介绍所有步骤。我意识到这对这里的某些开发人员来说可能有点矫枉过正,但它可能会对您有所帮助。
根据您放置源代码的位置,您需要设置环境变量 GO111MODULE
。从 go 1.11 开始,建议不要再将源代码放在 $GOPATH 中。如果你把你的代码放在那里并想使用 Go 模块,你必须将 GO111MODULE
设置为 true
(我个人将我所有的 Go 代码都放在 $GOPATH 之外)。在 Windows 上,您必须先创建环境变量并进行相应设置(并重新启动终端)。
要创建 Go 模块,您必须 运行 命令 go mod init <name of your module>
。在我的例子中,我 运行 命令 go mod init github.com/retgits/bla
创建了一个 go.mod
文件
module github.com/retgits/bla
go 1.12
现在添加模块就像 运行 宁 go get
一样简单。如果你想使用GoCenter或Artifactory来帮助解决你的模块。
为了帮助解析模块,您可能需要查看两个选项:
- 使用名为
goc
的实用程序
- 设置远程和虚拟代理
使用goc
goc 实用程序自动将 GOPROXY 设置为 GoCenter,绕过其他代理,如 Artifactory。 Go 客户端的当前行为是查看一个代理,如果不是所有模块都从那里解析,则构建失败。 goc
将首先查看 GoCenter,如果模块不在 GoCenter 中,它将从其原始位置获取模块(如 GitHub)
使用 Artifactory
如果您想使用 Artifactory 来解析和缓存您的 Go 模块,您必须使用 GoCenter 的设置创建一个远程存储库(如您所提到的,它在文档中)。您必须将该远程存储库包含到虚拟存储库中才能正常工作。
将 GoCenter 设置为 GOPROXY
第三种选择是仅使用 GoCenter(由 JFrog 创建的 public 注册表),但这可能会打败您最初的问题:
export GOPROXY="https://gocenter.io"
go get -v github.com/naoina/go-stringutil
无论您选择哪个选项,它都会将 go.mod
更新为
module github.com/retgits/bla
go 1.12
require github.com/naoina/go-stringutil v0.1.0 // indirect
indirect
是因为我还没有用使用此导入的代码创建 .go
文件。
如果我现在用
创建一个 main.go
样本
package main
import (
"fmt"
"github.com/naoina/go-stringutil"
)
func main() {
str := stringutil.ToSnakeCase("HelloWorld")
fmt.Println(str)
}
// Running go run main.go would result in
// hello_world
它将从 go.mod 文件中删除 indirect
,因为我现在有了依赖于我的模块的代码。
我是新手,正在尝试使用 gocenter 远程存储库 (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 回购定义的 t运行 版本。我使用 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
如果能帮助指出我做错了什么,我们将不胜感激。我还没有进入模块或类似的东西,因为我是一个 artifactory 管理员而不是开发人员。我只是在尝试制作此功能的原型,以便我们可以帮助建议用户如何操作。
--更新:我找到了 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 ':'
所以我的协议和 url 之间的冒号是无效字符?另外为什么要删除我的一个正斜杠?
--更新 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 开发人员,所以让我向您介绍所有步骤。我意识到这对这里的某些开发人员来说可能有点矫枉过正,但它可能会对您有所帮助。
根据您放置源代码的位置,您需要设置环境变量 GO111MODULE
。从 go 1.11 开始,建议不要再将源代码放在 $GOPATH 中。如果你把你的代码放在那里并想使用 Go 模块,你必须将 GO111MODULE
设置为 true
(我个人将我所有的 Go 代码都放在 $GOPATH 之外)。在 Windows 上,您必须先创建环境变量并进行相应设置(并重新启动终端)。
要创建 Go 模块,您必须 运行 命令 go mod init <name of your module>
。在我的例子中,我 运行 命令 go mod init github.com/retgits/bla
创建了一个 go.mod
文件
module github.com/retgits/bla
go 1.12
现在添加模块就像 运行 宁 go get
一样简单。如果你想使用GoCenter或Artifactory来帮助解决你的模块。
为了帮助解析模块,您可能需要查看两个选项:
- 使用名为
goc
的实用程序
- 设置远程和虚拟代理
使用goc
goc 实用程序自动将 GOPROXY 设置为 GoCenter,绕过其他代理,如 Artifactory。 Go 客户端的当前行为是查看一个代理,如果不是所有模块都从那里解析,则构建失败。 goc
将首先查看 GoCenter,如果模块不在 GoCenter 中,它将从其原始位置获取模块(如 GitHub)
使用 Artifactory
如果您想使用 Artifactory 来解析和缓存您的 Go 模块,您必须使用 GoCenter 的设置创建一个远程存储库(如您所提到的,它在文档中)。您必须将该远程存储库包含到虚拟存储库中才能正常工作。
将 GoCenter 设置为 GOPROXY
第三种选择是仅使用 GoCenter(由 JFrog 创建的 public 注册表),但这可能会打败您最初的问题:
export GOPROXY="https://gocenter.io"
go get -v github.com/naoina/go-stringutil
无论您选择哪个选项,它都会将 go.mod
更新为
module github.com/retgits/bla
go 1.12
require github.com/naoina/go-stringutil v0.1.0 // indirect
indirect
是因为我还没有用使用此导入的代码创建 .go
文件。
如果我现在用
创建一个main.go
样本
package main
import (
"fmt"
"github.com/naoina/go-stringutil"
)
func main() {
str := stringutil.ToSnakeCase("HelloWorld")
fmt.Println(str)
}
// Running go run main.go would result in
// hello_world
它将从 go.mod 文件中删除 indirect
,因为我现在有了依赖于我的模块的代码。