Go:如何獲得項目根目錄?
大家好,我是 polarisxu。
項目中,特別是 Web 項目,經常需要獲得項目的根目錄,進而可以訪問到項目相關的其他資源,比如配置文件、靜態資源文件、模板文件、數據文件、日志文件等(Go1.16 后,有些可以方便的通過 embed 內嵌進來)。比如下面的目錄結構:(路徑是 /Users/xuxinhua/stdcwd)
- ├── bin
- ├── cwd
- ├── main.go
- └── log
- ├── error.log
為了正確讀取 error.log,我們需要獲得項目根目錄。學完本文知識可以解決該問題。
解決方案有多種,各有優缺點和使用注意事項,選擇你喜歡的即可。
01 使用 os.Getwd
Go 語言標準庫 os 中有一個函數 Getwd:
- func Getwd() (dir string, err error)
它返回當前工作目錄。
基于此,我們可以得到項目根目錄。還是上面的目錄結構,切換到 /Users/xuxinhua/stdcwd,然后執行程序:
- $ cd /Users/xuxinhua/stdcwd
- $ bin/cwd
這時,當前目錄(os.Getwd 的返回值)就是 /Users/xuxinhua/stdcwd。
但是,如果我們不在這個目錄執行的 bin/cwd,當前目錄就變了。因此,這不是一種好的方式。
不過,我們可以要求必須在 /Users/xuxinhua/stdcwd 目錄運行程序,否則報錯,具體怎么做到限制,留給你思考。
02 使用 exec.LookPath
在上面的目錄結構中,如果我們能夠獲得程序 cwd 所在目錄,也就相當于獲得了項目根目錄。
- binary, err := exec.LookPath(os.Args[0])
os.Args[0] 是當前程序名。如果我們在項目根目錄執行程序 bin/cwd,以上程序返回的 binary 結果是 bin/cwd,即程序 cwd 的相對路徑,可以通過 filepath.Abs() 函數得到絕對路徑,最后通過調用兩次 filepath.Dir 得到項目根目錄。
- binary, _ := exec.LookPath(os.Args[0])
- root := filepath.Dir(filepath.Dir(filepath.Abs(binary)))
03 使用 os.Executable
可能是類似的需求很常見,Go 在 1.8 專門為這樣的需求增加了一個函數:
- // Executable returns the path name for the executable that started the current process.
- // There is no guarantee that the path is still pointing to the correct executable.
- // If a symlink was used to start the process, depending on the operating system, the result might be the symlink or the path it pointed to.
- // If a stable result is needed, path/filepath.EvalSymlinks might help.
- // Executable returns an absolute path unless an error occurred.
- // The main use case is finding resources located relative to an executable.
- func Executable() (string, error)
和 exec.LookPath 類似,不過該函數返回的結果是絕對路徑。因此,不需要經過 filepath.Abs 處理。
- binary, _ := os.Executable()
- root := filepath.Dir(filepath.Dir(binary))
注意,exec.LookPath 和 os.Executable 的結果都是可執行程序的路徑,包括可執行程序本身,比如 /Users/xuxinhua/stdcwd/bin/cwd
細心的讀者可能會注意到該函數注釋中提到符號鏈接問題,為了獲得穩定的結果,我們應該借助 filepath.EvalSymlinks 進行處理。
- package main
- import (
- "fmt"
- "os"
- "path/filepath"
- )
- func main() {
- ex, err := os.Executable()
- if err != nil {
- panic(err)
- }
- exPath := filepath.Dir(ex)
- realPath, err := filepath.EvalSymlinks(exPath)
- if err != nil {
- panic(err)
- }
- fmt.Println(filepath.Dir(realPath))
- }
最后輸出的就是項目根目錄。(如果你的可執行文件放在根目錄下,最后的 filepath.Dir 就不需要了)
注意:exec.LookPath 也有軟鏈接的問題。
exec.LookPath 和 os.Executable 函數,再提醒一點,如果使用 go run 方式運行,結果會是臨時文件。因此,記得先編譯(這也是比 go run 更好的方式,go run 應該只是用來本地測試)。
04 小結
既然 Go1.8 為我們專門提供了這樣一個函數,針對本文提到的場景,我們應該總是使用它。