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 maixpy训练模型,模型训练好了以后,开发板通电会报错,不知道是什么问题
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容