Skip to content

Instantly share code, notes, and snippets.

@kemokemo
Last active April 25, 2018 12:43
Show Gist options
  • Save kemokemo/4dd51ec10ed5c327328ed230e36ccff2 to your computer and use it in GitHub Desktop.
Save kemokemo/4dd51ec10ed5c327328ed230e36ccff2 to your computer and use it in GitHub Desktop.
a sample to get the current directory of the executed program.
package main
import (
"log"
"os"
"path/filepath"
)
const (
exitCodeOK int = iota
exitCodeFailed
)
var (
fullpath string
cd string
)
func init() {
var err error
fullpath, err = os.Executable()
if err != nil {
log.Println("Failed to initialize", err)
}
cd = filepath.Dir(fullpath)
}
func main() {
os.Exit(run(os.Args))
}
func run(args []string) int {
err := work()
if err != nil {
log.Println("Failed to execute the work:", err)
return exitCodeFailed
}
return exitCodeOK
}
func work() error {
log.Println("fullpath is ", fullpath)
log.Println("cd is ", cd)
return nil
}
@kemokemo
Copy link
Author

A sample that saves the full path of the program and the directory path of the program in the variable at startup. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment