从go get获取的外部包。

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

external package from go get

问题

我想使用code.google.com/p/google-api-go-client/drive/v2和其他包。

我的应用程序结构如下:

+-- MyApp
+---- app.yaml
+---- main.go
+---- src/
+------ ...外部包...

我的GOPATH等于"MyApp/src"。

在我的main.go文件中,我有// +build !appengine"

我无法启动goapp serve,我得到以下错误信息:

2014/12/09 22:20:32 在$GOPATH中找不到包"code.google.com/p/google-api-go-client/googleapi":无法在以下任何位置找到包"code.google.com/p/google-api-go-client/googleapi":

还有其他类似的错误信息。

我该如何使用通过go get下载的包?

谢谢。

英文:

I want use code.google.com/p/google-api-go-client/drive/v2 and other.

My app is structured like:

+-- MyApp
+---- app.yaml
+---- main.go
+---- src/
+------ ...external package...

My GOPATH is equal to "MyApp/src"

In my main.go I have `// +build !appengine"

I can't launch goapp serve, I get

2014/12/09 22:20:32 Can't find package "code.google.com/p/google-api-go-client/googleapi" in $GOPATH: cannot find package "code.google.com/p/google-api-go-client/googleapi" in any of:

and many other who said the same.

How I can use package download from a go get?

Thank you.

答案1

得分: 3

通常,一个gopath的结构如下所示:(我添加了一些随机项目来演示它可能的样子)

  • gopath
    • src
      • code.google.com
        • p
          • google-api-go-client
            • 等等
      • github.com
        • fluffle
          • goirc
      • rohan.com <- 这是你自己的项目所在的位置(或者在code.google.com或github.com中)
        • my_random_project
          • main.go
          • helper.go
        • my_app_engine_project
          • app
            • app.yaml
            • my_app_engine_project.go
          • routes
            • random_rest_route.go
          • process
            • random_route_logic.go
    • pkg
    • bin

你的$GOPATH$环境变量应该指向包含src、pkg和bin的根文件夹。

所以当你使用go get从github获取一个包时,它将被放置在github.com的src文件夹中,然后你就可以在自己的项目中使用该库了。

使用示例

例如,如果我需要从github获取fluffle/goirc库,我会输入:

go get github.com/fluffle/goirc

该库将被放置在:

gopath/src/github.com

然后,我可以通过导入它来使用该库:

import (
     "github.com/fluffle/goirc/client"
)

然后使用它:

client.NewConfig("My User Name")
英文:

Typically a gopath looks like this: (I have added random projects to it to demonstrate what it could look like)

  • gopath
    • src
      • code.google.com
        • p
          • google-api-go-client
            • etc
      • github.com
        • fluffle
          • goirc
      • rohan.com <- This is where your own projects go(or in code.google.com or github.com)
        • my_random_project
          • main.go
          • helper.go
          • my_app_engine_project
            • app
              • app.yaml
              • my_app_engine_project.go
            • routes
              • random_rest_route.go
            • process
              • random_route_logic.go
    • pkg
    • bin

Your $GOPATH$ enviroment variable should point to that root folder which contains src, pkg and bin.

So when you go get a package from github for example it'l be put in the github.com src folder, and that's when you'll be able to use that library in your own projects.

Usage Example

So for example, if I need the fluffle/goirc library from github, I'l type:

go get github.com/fluffle/goirc

The library will then be placed in:

gopath/src/github.com

And I can use the library by importing it with:

import (
     &quot;github.com/fluffle/goirc/client&quot;
)

And then use it

client.NewConfig(&quot;My User Name&quot;)

huangapple
  • 本文由 发表于 2014年12月10日 05:28:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/27389164.html
匿名

发表评论

匿名网友

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

确定