douhun7609 2014-05-05 10:12
浏览 29
已采纳

是否建议将程序源(而不是lib源)保存在单个文件中?

I am making my first steps into go and obviously am reasoning from what I'm used to in other languages rather than understanding go specificity and styles yet.

I've decided to rewrite a ruby background job I have that takes ages to execute. It iterates over a huge table in my database and process data individually for each row, so it's a good candidate for parallelization.

Coming from a ruby on rails task and using orm, this was meant to be, as I thought of it, a quite simple two files program : one that would contain a struct type and its methods to represent and work with a row and a main file to operate the database query and loop on rows (maybe a third file to abstract database access logic if it gets too heavy in my main file). This file separation as I intended it was meant for codebase clarity more than having any relevance in final binary.

I've read and seen several things on the topic, including questions and answers here, and it always tend to resolve into writing code as libraries, installing them and then using them into a single file source (in package main) program.

I've read that one may pass multiple files to go build / run, but it complains if there is several package name (so basically, everything should be in main) and it doesn't seem that common.

So, my questions are :

  • did i get it right, and having code mostly as library with a single file program importing it the way to go ?
  • if so, how do you deal with having to build libraries repeatedly ? Do you build / install on each change in library codebase before executing (which is way less convenient that what go run promise to be) or is there something common I don't know of to execute library dependent program quick and fast while working on those libraries code ?
  • 写回答

2条回答 默认 最新

  • douzhou7037 2014-05-05 11:14
    关注

    No.

    Go and the go tool works on packages only (just go run works on files, but that is a different story): You should not think about files when organizing Go code but packages. A package may be split into several files, but that is used for keeping test code separated and limiting file size or grouping types, methods, functions, etc.

    Your questions:

    did i get it right, and having code mostly as library with a single file program importing it the way to go ?

    No. Sometimes this has advantages, sometimes not. Sometimes a split may be one lib + one short main, in other cases just one large main might be better. Again: It is all about packages and never about files. There is nothing wrong with a single 12 file main package if this is a real standalone program. But maybe extracting some stuff into one or a few other packages might result in a more readable code. It all depends.

    if so, how do you deal with having to build libraries repeatedly ? Do you build / install on each change in library codebase before executing (which is way less convenient that what go run promise to be) or is there something common I don't know of to execute library dependent program quick and fast while working on those libraries code ?

    The go tool tracks the dependencies and recompiles whatever is necessary. Say you have a package main in main.go which imports a package foo. If you execute go run main.go it will recompile package foo transparently iff needed. So for quick hacks: No need for a two-step go install foo; go run main. Once you extract code into three packages foo, bar and waz it might be a bit faster to install foo, bar and waz.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法