doudou0111 2013-05-26 14:33 采纳率: 0%
浏览 65
已采纳

在应用程序中导入本地库和文件

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条回答 默认 最新

  • douchenchepan6465 2013-05-26 14:39
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料