In some cases is more handy to pass data as enviroment variables to my program instead of using flags for such. That is why I started to look how to do this. Here is an example:
package main
import (
"fmt"
"os"
"strings"
)
func main() {
// Sets the enviroment variable
os.Setenv("OUTPUT", "Blablabla")
// Gets the enviroment variable
fmt.Println("SCRIPT-OUTPUT:", os.Getenv("OUTPUT"))
// Gets the enviroment variable
fmt.Println("SHELL:", os.Getenv("SHELL"))
fmt.Println()
for _, env := range os.Environ() {
// Slices "env" into substrings.
pair := strings.SplitN(env, "=", 2)
// Prints the first slice. Example: From "USER=rabocse", it prints only "USER"
fmt.Println(pair[0])
// Prints the second slice. Example, From "USER=rabocse", it prints only "rabocse"
fmt.Println(pair[1])
}
}
BONUS: I got familiar with “SpintN”. Quite handy! :)
Documentation link: https://gobyexample.com/environment-variables