Golang: Get Script Path

By Xah Lee. Date: .

Here's how to get the current running script's path or name.

use

import "os"

like this:

path, err := os.Executable()

package main

import "fmt"
import "os"

// scriptPath returns the current running script path
func scriptPath() string {
	path, errPath := os.Executable()
	if errPath != nil {
		panic(errPath)
	}
	return path
}

func main() {

	fmt.Printf("%v\n", scriptPath())

	// sample output
	// /var/folders/fr/m8xpf1_545dgcvs2t0znb1f40000gp/T/go-build978213921/command-line-arguments/_obj/exe/get_path

}

If you have a question, put $5 at patreon and message me.

Golang

Examples

Reference