golang 读取运行程序的相关目录

发布时间 2023-08-20 19:20:18作者: 蓝色牧客

获取运行程序的所在目录、工作目录

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	fmt.Println("start m1")
	path, _ := os.Executable()
	fmt.Println("path", filepath.Dir(path))
	
	dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
	fmt.Println("os.Args", dir)
	
	dir, _ = os.Getwd()
	fmt.Println("Getwd", dir)
	
	dir, _ = filepath.Abs("./")
	fmt.Println("./", dir)
}

/*
start m1
path D:\Workspaces\golang-demo23\mt76
os.Args D:\Workspaces\golang-demo23\mt76
Getwd D:\Workspaces\golang-demo23\mt76
./ D:\Workspaces\golang-demo23\mt76
*/