Go/Golang Run embedded bash script or node/js,python,php,ruby etc

Since Go v1.16 there’s the embed package, a simple tool to embed files as filesystems of simple strings or []bytes.

While it has its downsides, I was recently met with a challenge. I’d like to run bash scripts I wrote because it’s more efficient to just run a bash script than breaking my fingers writing os/exec stuff.

Anyhow it’s pretty simple really. Any shell command has a standard input and output. You just assign the file pointer (aka the Reader) to the os.Command’s Stdin.

Example:
Imagine the following directory structure

bash/test.bash

install.go

Output:
Hello Sunny, this is a test

Imagine the possibilities. You can now embed PHP, JS, all kinds of scripting languages in Go and execute those scripts without ever needing to write any of the embedded content to disk. Of course you need the interpreters installed, but if you have enough time on your hands maybe you could figure out how to embed a scripting language and virtually pipe a script to an interpreter.

And while that’s nice, I do have a problem. How do you pass arguments like this?

Spread the love
Published
Categorized as Allgemein

By Darko Luketic

I'm a self-employed developer since 2004. I have been working professionally since 2000. My first contact with computers was the C64 at the age of 11 where I coded in machine code and assembler. I worked with PHP professionally for 11 years, nowadays I prefer Angular, Go and Flutter, but I'm open minded. However I would dislike taking a step back to PHP. My services are for hire I'm also looking for remote employment.

Leave a comment

Your email address will not be published. Required fields are marked *