doudai3012 2012-05-31 17:35
浏览 989
已采纳

如何编译由多个文件组成的Go程序?

So I have a small program that consists of three files, all belonging to the same package (main), but when I do "go build main.go" the build doesn't succeed. When it was just one file (main.go) everything worked fine. Now that I took some effort to separate the code, it looks like the compiler is unable to find the stuff that was taken out of main.go and put into these two other files (that reside at the same directory as the main.go). Which results in "undefined 'type'" errors.

I read something about setting the GOPATH variable, so I tried

set GOPATH=%cd%
go build main.go

but that too didn't work.

Any help is appreciated.

  • 写回答

6条回答

  • dongma7796 2012-05-31 17:43
    关注

    Supposing you're writing a program called myprog :

    Put all your files in a directory like this

    myproject/go/src/myprog/xxx.go
    

    Then add myproject/go to GOPATH

    And run

    go install myprog
    

    This way you'll be able to add other packages and programs in myproject/go/src if you want.

    Reference : http://golang.org/doc/code.html

    (this doc is always missed by newcomers, and often ill-understood at first. It should receive the greatest attention of the Go team IMO)

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

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办