1. 获取以后文件门路
import (
"errors"
"runtime"
)
func CurrentFile() string {_, file, _, ok := runtime.Caller(1)
if !ok {panic(errors.New("Can not get current file info"))
}
return file
}
2. 获取可执行文件目录
path, err := os.Getwd()
if err != nil {panic(err)
return
}
3. 获取以后文件所在目录
func CurrentFileDir() string {_, file, _, ok := runtime.Caller(1)
if !ok {return "失败"}
i := strings.LastIndex(file, "/") // linux 环境
if i < 0 {i = strings.LastIndex(file, "\\") // windows 环境
}
return string(file[0 : i+1])
}