使用golang导入子目录的函数。

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

golang using functions of imported subdirectories

问题

我无法使用自定义子目录的函数。

我的代码组织

在“src”下,我有一个路径层次结构,如下所示:

a/b

其中包含了所有的目录和go文件(它是我的项目的“根”)。这些目录不包含子目录,一切都正常运行。所以最深的路径是“a/b/c”。例如,我有

a/b/c

a/b/d

其中包含一些go文件。在“a/b/c”目录中的文件中导入“a/b/d”并调用函数“d.DoSomething()”是可以正常工作的。

问题描述

现在我想重新组织“a/b/d”。我将一些文件从“a/b/d”移动到

a/b/d/e

并将其余的文件移动到

a/b/d/f

如果我尝试从“/a/b/c”目录中的同一文件中使用import语句导入“a/b/d/e”,并调用“e.DoSomething()”(这是文件移动到的位置),我会在调用“e.DoSomething()”的那一行出现错误:“undefined: e”。

在寻找结果时,我从未见过具有更深路径层次结构的示例。是不是一般情况下无法使用/导入子目录,或者有什么问题?

我使用的go版本是:go1.2.2 linux/amd64


谢谢任何建议。

英文:

I can't use functions of custom subdirectories.

My Code Organziation

I have under "src" a path hierarchy like

> a/b

with all my directories and go-Files (it is the "root" of my project). The directories contain no subdirectories and it works fine. So the deepest path is "a/b/c". E.g. I have
> a/b/c

and

> a/b/d

with some go-files. Import of "a/b/d" and calling a function with "d.DoSomething()" from a file in "a/b/c" works fine.

Problem description

Now I want ot reorganize "a/b/d". I move some files from "a/b/d" to

> a/b/d/e

and the rest of the files to

> a/b/d/f

If try to import "a/b/d/e" with import-statement

> import ( "a/b/d/e" )

from the same file in "/a/b/c" and want to call "e.DoSomething()" (it is the place, where the file with the "DoSomething-function" moved to), I get an error at the line, where I call "e.DoSomething()": "undefined: e".

While searching for a result, I've nowhere seen examples with deeper path hierarchies. Is it generally not possible to use/import subdirectories or what's the problem?

go-version I used: go1.2.2 linux/amd64


Thanks for any advices

答案1

得分: 6

你的方法完全错误。Go语言没有导入文件或目录的概念,你只能导入包。现在的情况是,包的名称就是相对于GOPATH的路径,并且你通过该名称导入包。但是,导入的包在导入代码中的可用标识符取决于包的声明。你不能简单地将文件“移动”到其他目录,因为每个目录(对于go工具而言)都是一个单独的包,而不会改变包的声明。

你可以在路径a/b/c下有一个package x。当你使用import ("a/b/c")导入包x时,包x中的所有导出内容都可以通过x.ExportedName来使用。

请仔细阅读http://blog.golang.org/organizing-go-code。

英文:

Your approach is completely wrong. Go has absolutely no concept of importing files or directories, all you can import in Go are packages. It now happens that the name of a package is it's path relative to GOPATH and you import packages by that name. But the identifier under which an imported package is available in the importing code depends on the package declaration of the package. You cannot simply "move" files between directories as each directory (for the go tool) is a single package without changing the package declaration.

You can have package x under path a/b/c. When you import package x with import ( "a/b/c" ) all the exported stuff from package x is available to you as x.ExportedName.

Please read http://blog.golang.org/organizing-go-code carefully.

答案2

得分: 0

在尝试在a/b中构建之前,先尝试在a/b/d/e中进行go build操作,这样可以生成你想要导入的编译类。

英文:

Try and do a go build in a/b/d/e first, before trying to build in a/b: that will generate the compiled classes you want to import.

huangapple
  • 本文由 发表于 2014年8月10日 21:23:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/25229018.html
匿名

发表评论

匿名网友

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

确定