dongzouhe9734 2018-04-23 06:54
浏览 133
已采纳

如何在golang中使用本地包? [重复]

This question already has an answer here:

Here are two .go files.

├── lib.go
└── main.go

The lib.go has a package libtest.

$ cat lib.go 

package libtest

import (
    "fmt"
)

func TestLibFunc() {
    fmt.Println("This is test library function")
}

The main.go has a package main.

$ cat main.go
package main

import (
    "libtest"
)

func main() {
    libtest.TestLibFunc()
}

When I tried to build them, but it's failed.

$ go build *.go
can't load package: package main: found packages libtest (lib.go) and main (main.go) in /Users/dev/work/tmp/local-package

How can I use local packages in main package?

</div>
  • 写回答

2条回答 默认 最新

  • duan41497 2018-04-23 07:00
    关注

    You need to put each package in a separate sub-directory. So your directory structure so put lib.go in project/lib and main.go in project/cmd. It should then work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料