Golang: Get Script Path

By Xah Lee. Date: . Last updated: .

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

use

path, err := os.Executable()

package main

import "fmt"
import "os"

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

func main() {

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

	// sample outputs

	// mac
	// /var/folders/4y/y8f5s_mn585fsk3zkhn23w500000gn/T/go-build4002280626/b001/exe/x20220724_1758_ced

	// windows
	// C:/Users/xah/AppData/Local/Temp/go-build986769622/ b001/exe/x20220724_1154_99a.exe

}

Golang: Print Runtime Info