doujie4050 2016-09-15 12:09
浏览 702
已采纳

如何在GO中另一个包中的文件中使用主包中的函数?

Hi I want to call a method inside the main package, my project struct is like this: src:

  • go files : package main
  • Postgres folder:

    • go files: postgres package

now I want to call a method inside main package from the go files inside the postgres folder that is from postgres package. I tried to import "foo/src" then using src.Myfunction but I got an error:

import "foo/src" is a program, not an importable package
  • 写回答

1条回答 默认 最新

  • doubaben7394 2016-09-15 12:33
    关注

    The package main is supposed to be used only to implement the binary/command specific code. It usually imports code from other packages to glue everything together. If you need to import something from the package main, that code is probably not specific to that command, so it should belong to another package. After you refactor the code, you can import it from the package main and from your other package which also requires it.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?