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 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题