Tuesday, May 13, 2025
HomeGolang`go run <non-main-package>` -- why not?

`go run <non-main-package>` — why not?


Presently, for go run <arg> to work, the <arg> will need to have a bundle fundamental and a func fundamental. If it has the latter, however not the previous, you get error:

bundle command-line-arguments isn't a fundamental bundle

Why not change go run to permit it to run any bundle that has a func fundamental?
This wants 2 adjustments to the go run <arg> command:

  1. if resolves to a bundle path and that bundle has a func fundamental, run that.
  2. in any other case (present conduct), look in for a fundamental bundle with a func fundamental and run that or, if not discovered, give an error. The error must be modified from:
bundle command-line-arguments isn't a fundamental bundle

to

bundle command-line-arguments doesn't have a fundamental operate and isn't a fundamental bundle

(or phrases to that impact)

Execs:

  • makes go toolchain extra common, and, I assert, easier. Removes what seems to be an arbitrary restriction (“func fundamental solely means command entry level if it’s in bundle fundamental”)
  • is a backward appropriate extension of the API. Doesn’t break any program that was beforehand working.

Cons:

  • There is likely to be some current cmd tasks that have already got fundamental() in non-main packages. This proposal wouldn’t break any current manner of invoking such a mission (since all of them undergo bundle fundamental). But it surely may conceivably trigger a take a look at to fail, e.g the mission had an specific take a look at for an error from go run <non-main-package>. So there are code adjustments {that a} mission may need to make merely to retain all-tests-pass standing if the proposal had been carried out.

Right here’s my use case:
I’ve a set of documentation samples, every of which is a brief, standalone program (that means reader may run it by a go run command). I wish to preserve the sources in a single mission (i.e. in a single module). How ought to I construction the mission so I can run any chosen pattern?

  1. I may put every pattern in a separate bundle however with a func fundamental, then implement a top-level driver that is aware of find out how to invoke every particular person code pattern bundle. The invocation can be one thing like go run driver <pattern>.
    There’s the redundancy of getting to offer this driver and likewise of getting to place every pattern program in a separate subdirectory as a result of it’s in a separate bundle.
  2. (What I’m at present doing as a workaround) I preserve every pattern program in a separate supply file (within the mission root). Every file has bundle fundamental and func fundamental.
    I can invoke the chosen pattern by way of go run <pattern>.go (word it’s a file path). I can’t go take a look at or go construct the module (due to duplicate definitions of func fundamental), however I stay with that for now.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments