英文:
Importing local library and files in an application
问题
我是新手学习Go语言(但不是编程新手),我喜欢这门语言,但是我对于如何通过包来创建应用程序中的内部库还有一些困惑。关于获取外部包并导入/使用它们的部分我已经理解了。
假设我正在创建一个名为A的应用程序。
/home/me/A/a.go (包名为main)
然后,我意识到a.go变得相当庞大,所以我将它分成两部分。
/home/me/A/a.go (包名为main)
/home/me/A/b.go (包名为main)
我应该如何从a.go中导入/包含b.go以使其函数可用?
作为问题的延伸,在A中我正在操作许多对象O,所以我想如果我给它们自己的包并将功能封装在一个公共/导出的API中会更好。我该如何做到这一点?
我尝试创建./lib/o.go
(包名为o)并使用import lib/o
,但是我一直收到类似的错误:
./a.go:6: imported and not used: "o"
./a.go:43: undefined: o
我在环境变量中没有设置GOPATH,但是我尝试了export GOPATH=$GOPATH:/home/me/A
,但结果没有改变。
我尝试阅读关于"Go布局"的文章,但一下子感觉有点太多了,我真的希望能够得到一个更简单的解释,只解释我正在尝试的这个“小”步骤。
谢谢!
GOPATH/src/me/a/a.go:
package main
func main() {
test()
}
GOPATH/src/me/a/test.go:
package main
import "fmt"
func test() {
fmt.Println("test func!")
}
执行:
$ go run a.go
# command-line-arguments
./a.go:4: undefined: test
编辑:在这里找到了答案:https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/qysy2bM_o1I
要么在go run中列出所有文件(go run a.go test.go
),要么使用go build
并运行生成的可执行文件。
英文:
I'm new to Go (but not at programming), I love the language but I have a bit of trouble fully understanding the way I'm supposed to make internal libraries in an application through packages. For reference, getting external packages and then importing/using them is fine.
Let's say I'm making an application A.
/home/me/A/a.go (package main)
Then, I realize a.go start to be rather big, so I cut it into two parts
/home/me/A/a.go (package main)
/home/me/A/b.go (package main)
How am I supposed to import/include b.go from a.go to make its function available ?
As a continuation of the question, in the A I'm manipulation lots of objects O, so I figure it would be a lot better if I just give them their own package and encapsulate the functionalities in a public/exported api. How do I do that ?
I've tried creating ./lib/o.go
(package o) and import lib/o
but I keep getting error like
./a.go:6: imported and not used: "o"
./a.go:43: undefined: o
I have no GOPATH in my env but I tried export GOPATH=$GOPATH:/home/me/A
and it didn't change the result.
I've tried to read the article on "go layout" but it felt a bit too overwhelming at once and I would really love a simpler explanation of that one "small" step I am trying to make
Thanks !
GOPATH/src/me/a/a.go:
package main
func main() {
test()
}
GOPATH/src/me/a/test.go:
package main
import "fmt"
func test() {
fmt.Println("test func !")
}
Exec:
$ go run a.go
# command-line-arguments
./a.go:4: undefined: test
EDIT: got my answer here: https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/qysy2bM_o1I
Either list all files in go run (go run a.go test.go
) or use go build
and run the resulting executable.
答案1
得分: 1
你在尝试使用Go构建系统,但没有遵循必要的目录布局。阅读这个文档会对你有很大帮助。
简而言之,以下是与go工具相关的阻碍因素:
-
你必须有一个有效的、已导出的GOPATH
-
导入路径为"example/foo"的包文件必须位于
$GOPATH/src/example/foo
目录中。
更多详细信息请参阅上述链接的文章。
英文:
You're trying to use the Go build system while not following the necessaary required directory layouts. You will benefit a lot from reading this document.
In short, these are, wrt the go tool, the show stoppers:
-
You must have a valid, exported GOPATH
-
Package files with import path "example/foo" must be located in the
$GOPATH/src/example/foo
directory.
For more details please see the above linked article.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论