在同一文件夹中导入Go文件。

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

importing go files in same folder

问题

我在将一个本地的go文件导入到另一个go文件中遇到了困难。

我的项目结构如下所示:

-samplego
--pkg
--src
---github.com
----xxxx
-----a.go
-----b.go
--bin

我试图在b.go中导入a.go。我尝试了以下方法:

import "a"
import "github.com/xxxx/a"

但是这些都没有起作用...我知道我需要调整GOPATH,但我搞不清楚。目前我的GOPATH指向samplego(/workspace/samplego)。我得到以下错误:

cannot find package "a" in any of:
/usr/local/go/src/pkg/a (from $GOROOT)
/workspace/samplego/src/a (from $GOPATH)

此外,当这些源文件被导入到另一个项目/模块中时,GOPATH是如何工作的?本地导入会成为一个问题吗?在这种情况下,最佳实践是什么 - 是只有一个go文件在模块中(带有相关的测试)?

英文:

I am having difficulty in importing a local go file into another go file.

My project structure is like something below

-samplego
--pkg
--src
---github.com
----xxxx
-----a.go
-----b.go
--bin

I am trying to import a.go inside b.go. I tried the following,

import "a"
import "github.com/xxxx/a"

None of these worked..I understand I have to meddle up with GOPATH but I couldn't get it right. Presently my GOPATH is pointing to samplego(/workspace/samplego).I get the below error

cannot find package "a" in any of:
/usr/local/go/src/pkg/a (from $GOROOT)
/workspace/samplego/src/a (from $GOPATH)

Also, how does GOPATH work when these source files are imported into another project/module? Would the local imports be an issue then? What is the best practice in this case - is it to have just one go file in module(with associated tests)?

答案1

得分: 117

一个目录中的任意数量的文件构成一个单一的包;在一个文件中声明的符号可以在其他文件中使用,无需任何import或限定符。所有文件都需要在顶部有相同的package foo声明(否则go build会报错)。

你需要将GOPATH设置为包含pkgsrcbin目录的目录。这只是个人偏好的问题,但通常会为所有应用程序使用一个单独的工作区(有时是$HOME),而不是每个应用程序一个工作区。

通常,Github 路径应该是github.com/username/reponame(而不仅仅是github.com/xxxx)。所以,如果你想要有main和另一个包,你可能需要在workspace/src下做一些事情,比如:

github.com/
  username/
    reponame/
      main.go   // package main, importing "github.com/username/reponame/b"
      b/
        b.go    // package b

请注意,你总是使用完整的github.com/...路径进行导入:在工作区中不允许使用相对导入。如果你厌倦了输入路径,可以使用goimports。如果你一直使用go run,那么现在是时候切换到go build了:run对于多文件的main处理得不好,我没有测试过,但从 Dave Cheney 这里听说go run不会重新构建脏依赖项。

听起来你至少尝试过将 GOPATH 设置为正确的值,所以如果你仍然卡住了,也许可以具体说明你如何设置环境变量(命令等),以及你运行了什么命令和发生了什么错误。这里有关于如何设置(并使设置持久化)Linux/UNIX下的GOPATH的说明,以及这里是Go团队关于工作区设置的建议。也许两者都没有帮助,但至少看一下,如果你有困惑的地方,请指出具体是哪个部分让你困惑。

英文:

Any number of files in a directory are a single package; symbols declared in one file are available to the others without any imports or qualifiers. All of the files do need the same package foo declaration at the top (or you'll get an error from go build).

You do need GOPATH set to the directory where your pkg, src, and bin directories reside. This is just a matter of preference, but it's common to have a single workspace for all your apps (sometimes $HOME), not one per app.

Normally a Github path would be github.com/username/reponame (not just github.com/xxxx). So if you want to have main and another package, you may end up doing something under workspace/src like

github.com/
  username/
    reponame/
      main.go   // package main, importing "github.com/username/reponame/b"
      b/
        b.go    // package b

Note you always import with the full github.com/... path: relative imports aren't allowed in a workspace. If you get tired of typing paths, use goimports. If you were getting by with go run, it's time to switch to go build: run deals poorly with multiple-file mains and I didn't bother to test but heard (from Dave Cheney here) go run doesn't rebuild dirty dependencies.

Sounds like you've at least tried to set GOPATH to the right thing, so if you're still stuck, maybe include exactly how you set the environment variable (the command, etc.) and what command you ran and what error happened. Here are instructions on how to set it (and make the setting persistent) under Linux/UNIX and here is the Go team's advice on workspace setup. Maybe neither helps, but take a look and at least point to which part confuses you if you're confused.

答案2

得分: 63

只要你声明a.gob.go在同一个包中,就不需要导入任何内容。然后,你可以使用go run命令来识别多个文件,例如:

$ go run a.go b.go
英文:

No import is necessary as long as you declare both a.go and b.go to be in the same package. Then, you can use go run to recognize multiple files with:

$ go run a.go b.go

答案3

得分: 20

在这种情况下:
>./main.go(在main包中)<br>
./a/a.go(在a包中)<br>
./a/b.go(在a包中)

main.go导入"./a"

它可以调用a.go和b.go中首字母大写的函数。

英文:

>./main.go (in package main) <br>
./a/a.go (in package a) <br>
./a/b.go (in package a)

in this case:<br>
main.go import "./a"

It can call the function in the a.go and b.go,that with first letter caps on.

答案4

得分: 8

如果以上的答案都不起作用,

只需尝试,

go run .

用于生产环境,

go build

这将处理文件夹中的所有 .go 文件。

英文:

If none of the above answers works,

Just try,

go run .

for production,

go build

This will take care of all the .go files in the folder.

答案5

得分: 2

我只是想要一个非常基本的东西,将一些文件从主文件夹中移出,就像user2889485的回答一样,但他的具体答案对我不起作用。我不在乎它们是否在同一个包中。

我的GOPATH工作空间是c:\work\go,在其中我有:

/src/pg/main.go      (包名为main)
/src/pg/dbtypes.go   (包名为dbtypes)

main.go中,我使用import "/pg/dbtypes"导入。

英文:

I just wanted something really basic to move some files out of the main folder, like user2889485's reply, but his specific answer didnt work for me. I didnt care if they were in the same package or not.

My GOPATH workspace is c:\work\go and under that I have

/src/pg/main.go      (package main)
/src/pg/dbtypes.go   (pakage dbtypes)

in main.go I import &quot;/pg/dbtypes&quot;

答案6

得分: 1

正如之前提到的,没有必要使用任何导入。
很多人提到,在提到大多数文件时,可以使用go run,但是当在同一个目录中有多个.go文件时,可能会很麻烦。
因此,我通常使用go run *.go

英文:

As people mentioned previously, there is no need to use any imports.
A lot of people mention that using go run is possibe when you mention most files, however when having multiple .go-files in the same dir it can be cumbersome.
Therefore using go run *.go is what I usually do.

答案7

得分: -1

根据我的理解,对于项目子文件夹中的包,现在可以这样做,只需要在模块前面添加“.”,例如
. "github.com/ilyasf/deadlock-train/common"
其中github.com/ilyasf/deadlock-train是我的主模块名称,common只是项目内/common子文件夹中的一个包。

英文:

As I understand for packages in your project subfolders it's possible now to do, just need to add "." in front of module, like
. &quot;github.com/ilyasf/deadlock-train/common&quot;
where github.com/ilyasf/deadlock-train my main module name and common is just package from /common subfolder inside project.

答案8

得分: -1

你好!以下是翻译好的内容:

go1.19.1 版本在这里。我刚遇到了同样的问题,发现你只需要这样做:import (a "github.com/xxxx")

英文:

go1.19.1 version here. I've just had the same issue, discovered that you must simply do: import (a &quot;github.com/xxxx&quot;)

答案9

得分: -1

你可以进入正确的文件夹并执行:$ go run *.go

只要所有文件中只有一个主函数,它就能完美运行!

英文:

You can go to the correct folder and execute: $ go run *.go

As long as the code only 1 main function in all files it works perfect!

huangapple
  • 本文由 发表于 2014年11月15日 11:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/26942150.html
匿名

发表评论

匿名网友

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

确定