Last active
April 25, 2018 12:43
-
-
Save kemokemo/4dd51ec10ed5c327328ed230e36ccff2 to your computer and use it in GitHub Desktop.
a sample to get the current directory of the executed program.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A sample that saves the full path of the program and the directory path of the program in the variable at startup. 😄