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条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样