1. 程式人生 > >go語言獲取exe檔案執行路徑

go語言獲取exe檔案執行路徑

// NewExec
package main

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

func main() {

	execPath, err := exec.LookPath(os.Args[0])
	if err != nil {
		log.Fatal(err)
	}
	//    Is Symlink
	fi, err := os.Lstat(execPath)
	if err != nil {
		log.Fatal(err)
	}
	if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
		execPath, err = os.Readlink(execPath)
		if err != nil {
			log.Fatal(err)
		}
	}
	execDir := filepath.Dir(execPath)
	if execDir == "." {
		execDir, err = os.Getwd()
		if err != nil {
			log.Fatal(err)
		}
	}
	fmt.Print(execDir)
	cmd := exec.Command(execDir + "/XML.exe")
	err = cmd.Run()
	if err != nil {
		fmt.Println(err)
	}
	cmd = exec.Command(execDir + "/MOW.exe")
	err = cmd.Run()
	if err != nil {
		fmt.Println(err)
	}

}