代码拉取完成,页面将自动刷新
package goutils
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"
)
// 空目录
func EmptyDir(dir string) bool {
dirs, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err)
return true
}
return len(dirs) == 0
}
// 非空目录
func NonEmptyDir(dir string) bool {
dirs, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err)
return false
}
return len(dirs) > 0
}
// file and folder
func FileExist(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
// file and folder
func FileNotExist(path string) bool {
_, err := os.Stat(path)
return os.IsNotExist(err)
}
// 确保完整路径
func EnsureDir(dir string, mode os.FileMode) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, mode); err != nil {
return fmt.Errorf("could not create directory %v. %v", dir, err)
}
}
return nil
}
// helper function to make dir creation independent of root dir
func Rootify(path string, roots ...string) string {
if filepath.IsAbs(path) {
return path
}
if len(roots) > 0 && len(roots[0]) > 0 {
return filepath.Join(roots[0], path)
}
p, err := filepath.Abs(path)
if err != nil {
panic(err)
}
return p
}
// DefaultDataDir is the default data directory
func DefaultDataDir(dirs ...string) string {
var dir string
if len(dirs) > 0 {
dir = dirs[0]
}
// Try to place the data folder in the user's home dir
home := HomeDir()
if home == "" {
return ""
}
switch runtime.GOOS {
case "windows":
// We used to put everything in %HOME%\AppData\Roaming, but this caused
// problems with non-typical setups. If this fallback location exists and
// is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
fallback := filepath.Join(home, "AppData", "Roaming", dir)
appdata := WindowsAppData()
if appdata == "" || NonEmptyDir(fallback) {
return fallback
}
return filepath.Join(appdata, dir)
case "darwin":
return filepath.Join(home, "Library", "Application Support", dir)
default:
return filepath.Join(home, dir)
}
}
func HomeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return ""
}
func WindowsAppData() string {
v := os.Getenv("LOCALAPPDATA")
if v == "" {
// Windows XP and below don't have LocalAppData. Crash here because
// we don't support Windows XP and undefining the variable will cause
// other issues.
panic("environment variable LocalAppData is undefined")
}
return v
}
//返回当前可执行文件的路径
//os.Getwd() 返回当前的 work dir 工作目录
func GetCurrentPath() string {
file, err := exec.LookPath(os.Args[0])
if err != nil {
panic(err)
}
path, err := filepath.Abs(file)
if err != nil {
panic(err)
}
index := strings.LastIndex(path, string(os.PathSeparator))
ret := path[:index]
return ret
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。