【GO】文件操作
文章目录
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])
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论