doujiao8491 2013-03-08 14:07
浏览 62
已采纳

无法理解如何在多个文件中编译go代码[重复]

This question already has an answer here:

OK, Go's major selling point is its ease of compilation and wonderful toolchain, but as a go newbie I'm really lost there and can't understand the documentation on that point.

I have a stack toy example within two files (one for the type definition and methods, called stack.go, one for the main program, called main.go), both are in my GOPATH/src/stacker directory.

  1. How should each file be named ? Does it have any importance at all ? Is there at least a convention ? A mandatory naming ?
  2. What should be the package name ? I understood they should use the same package name, but which one ? Is it stacker ?
  3. In main.go, how should I use the import directive to import stack.go ?

I have tried many combinations, none working until now.

</div>
  • 写回答

1条回答 默认 最新

  • doupin2013 2013-03-08 14:21
    关注
    1. You can name the files however you like, just beware of special suffixes like _test and _<arch> (_darwin, _unix, etc.). Also note that files prefixed with . or _ won't be compiled into the package!
    2. It is recommended that you name the package like the folder the file is in, although it's possible (but confusing) to name a package differently in the declaration package mypkg
    3. If stack.go is in the same folder/package as main.go, you don't need to import. Everything delcared in stack.go is already available in main.go, because it is in the same package.

    If stacker should compile into an executable, you should use package main.

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

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题