package main import ( "bufio" "log" "os" "os/exec" "path/filepath" "strings" "time" ) /*获取当前文件执行的路径*/ func GetCurrPath() string { file, _ := exec.LookPath(os.Args[0]) path, _ := filepath.Abs(file) splitstring := strings.Split(path, "\\") size := len(splitstring) splitstring = strings.Split(path, splitstring[size-1]) ret := strings.Replace(splitstring[0], "\\", "/", size-1) return ret } func main() { //创建日志文件 t := time.Now() filepath := "./log_" + strings.Replace(t.String()[:19], ":", "_", 3) + ".txt" file, err := os.OpenFile(filepath, os.O_CREATE, 0666) if err != nil { log.Fatal("create log file failed!") } defer file.Close() wFile := bufio.NewWriter(file) //获取当前程序执行的绝对路径 wFile.WriteString(GetCurrPath()) wFile.Flush() }
有疑问加站长微信联系(非本文作者)