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

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

path D:\Workspaces\golang-demo23\mt76\build
os.Args D:\Workspaces\golang-demo23\mt76\build
Getwd D:\Workspaces\golang-demo23\mt76
./ D:\Workspaces\golang-demo23\mt76

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)
}
posted @ 2023-08-20 19:12  蓝色牧客  阅读(132)  评论(0编辑  收藏  举报