Golang导入问题

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

Golang Importing Issue

问题

我正在尝试导入一个用于内部使用的包,但是遇到了一些问题。
我的目录结构如下:

app/
  model/
    file1.go
    file2.go
    ...
  main.go

当我尝试构建程序时,我得到了一个类似于以下错误的信息:

/usr/local/go/src/pkg/model (from $GOROOT)

我希望能够在我的其他程序中任意调用app中的model程序,只需使用以下导入语句:

import "app/model"

在这种情况下,我有哪些选项可供选择?

英文:

I'm trying to use import a package for internal use, but I have been having some issues.
My directory structure looks like this:

  app/
    model/
      file1.go
      file2.go
      ...
    main.go

When I try to build the program, I get an error that looks something like this:

/usr/local/go/src/pkg/model (from $GOROOT)

I want to be able to call the model programs in any of my other programs in the app simply using:

import "app/model"

What are my options when it comes to doing this?

答案1

得分: 2

你从GOPATH级别导入..你的所有包都应该放在那里。

例如,假设你的应用程序在这里:

$GOPATH/src/dtrinh100/app/

..而你希望导入的包在这里:

$GOPATH/src/github.com/other/package

你的导入语句应该是:

import "github.com/other/package"

你应该阅读有关GOPATH环境变量的文献。在开始学习Go时,了解其目的非常重要,最初应将所有项目/包放在GOPATH内。

英文:

You import from GOPATH level .. all of your packages should live there.

For example, assuming your application is here:

$GOPATH/src/dtrinh100/app/

..and your package you wish to import is here:

$GOPATH/src/github.com/other/package

Your import would be:

import "github.com/other/package"

You should review the literature around what the GOPATH environment variable is all about. When beginning Go, it is important you understand its purpose and initially, you should place all of your projects/packages inside of the GOPATH.

答案2

得分: 0

当你导入一个自定义包时,Go会在GOPATH环境变量中列出的每个工作区中查找其定义。你的自定义包应该定义在src子目录中。

如果你将代码保存在某个源代码仓库中,那么你应该使用该源代码仓库的根目录作为基本路径。例如,如果你在GitHub上有一个账户github.com/user,那么这就是你的基本路径。

请注意,在构建代码之前,你不需要将代码发布到远程仓库。这只是一个良好的习惯,以便将来有可能发布代码。实际上,你可以选择任意的路径名称,只要它在标准库和更大的Go生态系统中是唯一的。

你应该将github.com/user作为你的基本路径。在你的工作区内创建一个目录来保存源代码:

$ mkdir -p $GOPATH/src/github.com/user

你可以查看如何编写Go代码获取更多详细信息。

英文:

When you import a custom package, Go looks for its definition in each workspace listed in the GOPATH environment variable. Your custom package should be defined in a src subdirectory.

If you keep your code in a source repository somewhere, then you should use the root of that source repository as your base path. For instance, if you have a GitHub account at github.com/user, that should be your base path.

Note that you don't need to publish your code to a remote repository before you can build it. It's just a good habit to organize your code as if you will publish it someday. In practice you can choose any arbitrary path name, as long as it is unique to the standard library and greater Go ecosystem.

You should use github.com/user as our base path. Create a directory inside your workspace in which to keep source code:

$ mkdir -p $GOPATH/src/github.com/user

You can look at How to Write Go Code for more details.

huangapple
  • 本文由 发表于 2015年3月18日 12:24:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/29114110.html
匿名

发表评论

匿名网友

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

确定