How do I get Go to evaluate the $PATH variable. I currently just prints "$PATH"
I have the following code
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("echo","$PATH").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n",out)
}
You need to use os.Getenv("PATH")
package main
import (
"fmt"
"log"
"os/exec"
"os"
)
func main() {
out, err := exec.Command("echo",os.Getenv("PATH")).Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n",out)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With