Golo Roden
Golo Roden

Reputation: 150772

Writing a Go program that acts as an executable as well as a module

I am currently trying to do my first steps in Go. Now I've ported a tool that I once written in Node.js, and I was surprised how easy that was, and how clean and concise the code is.

Anyway, I've got a question that I was not able to figure out by myself so far: In Node.js it's possible to add the main entry as well as the bin entry to the package.json file. This basically means that you can create a module that works as a executable when installed using

$ npm install -g <module>

but as a library when installed using

$ npm install <module>

The trick here is that the first one uses the bin entry, which then internally uses a file from the module's lib folder, but the second version directly points to this lib file.

So ... now I would like to have the same behavior in Go: I would like to write a package that you can directly run as an executable, but that as well you can import into another application as a library. How would I do that?

Obviously I can't put two calls to package into a .go file. Any hints?

Upvotes: 0

Views: 150

Answers (1)

Related Questions