为什么我无法获取 Docker 的 Go SDK?

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

Why I am not able to fetch go sdk for docker ?

问题

我正在尝试运行Docker Go SDK示例文件。我对Go语言还很陌生,不太清楚如何运行下面的Docker SDK API代码。但我已经尝试了一些方法,请帮我解决以下错误。

当我尝试运行go run example-ps.go时,出现以下错误:

example-ps.go - 内容如下:

  1. package main
  2. import (
  3. "io"
  4. "os"
  5. "github.com/docker/docker/client"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/api/types/container"
  8. "golang.org/x/net/context"
  9. )
  10. func main() {
  11. ctx := context.Background()
  12. cli, err := client.NewEnvClient()
  13. if err != nil {
  14. panic(err)
  15. }
  16. _, err = cli.ImagePull(ctx, "docker.io/library/alpine", types.ImagePullOptions{})
  17. if err != nil {
  18. panic(err)
  19. }
  20. resp, err := cli.ContainerCreate(ctx, &container.Config{
  21. Image: "alpine",
  22. Cmd: []string{"echo", "hello world"},
  23. }, nil, nil, "")
  24. if err != nil {
  25. panic(err)
  26. }
  27. if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
  28. panic(err)
  29. }
  30. if _, err = cli.ContainerWait(ctx, resp.ID); err != nil {
  31. panic(err)
  32. }
  33. out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
  34. if err != nil {
  35. panic(err)
  36. }
  37. io.Copy(os.Stdout, out)
  38. }

我遇到了以下错误。我该如何导入这些库文件?

  1. example-ps.go:8:3: cannot find package "github.com/docker/docker/api/types" in any of:
  2. /usr/local/go/src/github.com/docker/docker/api/types (from $GOROOT)
  3. /Users/air/go/src/github.com/docker/docker/api/types (from $GOPATH)
  4. example-ps.go:9:3: cannot find package "github.com/docker/docker/api/types/container" in any of:
  5. /usr/local/go/src/github.com/docker/docker/api/types/container (from $GOROOT)
  6. /Users/air/go/src/github.com/docker/docker/api/types/container (from $GOPATH)
  7. example-ps.go:7:3: cannot find package "github.com/docker/docker/client" in any of:
  8. /usr/local/go/src/github.com/docker/docker/client (from $GOROOT)
  9. /Users/air/go/src/github.com/docker/docker/client (from $GOPATH)
  10. example-ps.go:10:3: cannot find package "golang.org/x/net/context" in any of:
  11. /usr/local/go/src/golang.org/x/net/context (from $GOROOT)
  12. /Users/air/go/src/golang.org/x/net/context (from $GOPATH)
英文:

I am trying to run docker go SDK example file. I am quite new to Go language; I am not sure how to run below docker SDK API code. But I tried somehow; please help me with below error.

When I tried to run this go run example-ps.go

example-ps.go - content is below

  1. package main
  2. import (
  3. "io"
  4. "os"
  5. "github.com/docker/docker/client"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/api/types/container"
  8. "golang.org/x/net/context"
  9. )
  10. func main() {
  11. ctx := context.Background()
  12. cli, err := client.NewEnvClient()
  13. if err != nil {
  14. panic(err)
  15. }
  16. _, err = cli.ImagePull(ctx, "docker.io/library/alpine", types.ImagePullOptions{})
  17. if err != nil {
  18. panic(err)
  19. }
  20. resp, err := cli.ContainerCreate(ctx, &container.Config{
  21. Image: "alpine",
  22. Cmd: []string{"echo", "hello world"},
  23. }, nil, nil, "")
  24. if err != nil {
  25. panic(err)
  26. }
  27. if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
  28. panic(err)
  29. }
  30. if _, err = cli.ContainerWait(ctx, resp.ID); err != nil {
  31. panic(err)
  32. }
  33. out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
  34. if err != nil {
  35. panic(err)
  36. }
  37. io.Copy(os.Stdout, out)
  38. }

I am getting below error. How do I import those library files?

  1. example-ps.go:8:3: cannot find package "github.com/docker/docker/api/types" in any of:
  2. /usr/local/go/src/github.com/docker/docker/api/types (from $GOROOT)
  3. /Users/air/go/src/github.com/docker/docker/api/types (from $GOPATH)
  4. example-ps.go:9:3: cannot find package "github.com/docker/docker/api/types/container" in any of:
  5. /usr/local/go/src/github.com/docker/docker/api/types/container (from $GOROOT)
  6. /Users/air/go/src/github.com/docker/docker/api/types/container (from $GOPATH)
  7. example-ps.go:7:3: cannot find package "github.com/docker/docker/client" in any of:
  8. /usr/local/go/src/github.com/docker/docker/client (from $GOROOT)
  9. /Users/air/go/src/github.com/docker/docker/client (from $GOPATH)
  10. example-ps.go:10:3: cannot find package "golang.org/x/net/context" in any of:
  11. /usr/local/go/src/golang.org/x/net/context (from $GOROOT)
  12. /Users/air/go/src/golang.org/x/net/context (from $GOPATH)

答案1

得分: 3

如果你已经正确安装了Golang,只需在你的Golang代码所在的目录中执行以下命令:

  1. go get -v -t ./...

这将安装上游库...然后你可以编译你的代码...要获取详细信息,请执行以下命令:

  1. go help packages

简而言之,./... 将安装当前目录中任何Go文件的import中提到的所有包(以及递归安装其上游包等等)。

如果你曾经花费数天手动安装其他语言的上游库,你会非常欣赏这个命令的便利性...是的,我在说你,C、C++、Java,尤其是Python,它有着最复杂、最不连贯的库导入机制。

英文:

if you have golang installed correctly just issue this in the dir where your golang code lives

  1. go get -v -t ./...

which will install the upstream libraries ... then you can compile your code ... for details issue

  1. go help packages

the short of it is ./... will install all packages [ and recursively its upstream packages, etc, etc.] mentioned in any go file's import in your current dir

If you have ever spent days manually installing upstream libraries for any other language you will appreciate how much of a lifesaver this really is ... Yes I'm looking at you c, c++, java and especially python which has the roughest most disjoint library import machination incantation alchemy

huangapple
  • 本文由 发表于 2017年8月1日 07:46:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/45426891.html
匿名

发表评论

匿名网友

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

确定