自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go:如何獲得項(xiàng)目根目錄?

開發(fā) 后端
項(xiàng)目中,特別是 Web 項(xiàng)目,經(jīng)常需要獲得項(xiàng)目的根目錄,進(jìn)而可以訪問到項(xiàng)目相關(guān)的其他資源,比如配置文件、靜態(tài)資源文件、模板文件、數(shù)據(jù)文件、日志文件等(Go1.16 后,有些可以方便的通過 embed 內(nèi)嵌進(jìn)來)。

[[432613]]

大家好,我是 polarisxu。

項(xiàng)目中,特別是 Web 項(xiàng)目,經(jīng)常需要獲得項(xiàng)目的根目錄,進(jìn)而可以訪問到項(xiàng)目相關(guān)的其他資源,比如配置文件、靜態(tài)資源文件、模板文件、數(shù)據(jù)文件、日志文件等(Go1.16 后,有些可以方便的通過 embed 內(nèi)嵌進(jìn)來)。比如下面的目錄結(jié)構(gòu):(路徑是 /Users/xuxinhua/stdcwd)

  1. ├── bin 
  2.     ├── cwd 
  3. ├── main.go 
  4. └── log 
  5.     ├── error.log 

為了正確讀取 error.log,我們需要獲得項(xiàng)目根目錄。學(xué)完本文知識(shí)可以解決該問題。

解決方案有多種,各有優(yōu)缺點(diǎn)和使用注意事項(xiàng),選擇你喜歡的即可。

01 使用 os.Getwd

Go 語言標(biāo)準(zhǔn)庫 os 中有一個(gè)函數(shù) Getwd:

  1. func Getwd() (dir string, err error) 

它返回當(dāng)前工作目錄。

基于此,我們可以得到項(xiàng)目根目錄。還是上面的目錄結(jié)構(gòu),切換到 /Users/xuxinhua/stdcwd,然后執(zhí)行程序:

  1. $ cd /Users/xuxinhua/stdcwd 
  2.  
  3. $ bin/cwd 

這時(shí),當(dāng)前目錄(os.Getwd 的返回值)就是 /Users/xuxinhua/stdcwd。

但是,如果我們不在這個(gè)目錄執(zhí)行的 bin/cwd,當(dāng)前目錄就變了。因此,這不是一種好的方式。

不過,我們可以要求必須在 /Users/xuxinhua/stdcwd 目錄運(yùn)行程序,否則報(bào)錯(cuò),具體怎么做到限制,留給你思考。

02 使用 exec.LookPath

在上面的目錄結(jié)構(gòu)中,如果我們能夠獲得程序 cwd 所在目錄,也就相當(dāng)于獲得了項(xiàng)目根目錄。

  1. binary, err := exec.LookPath(os.Args[0]) 

os.Args[0] 是當(dāng)前程序名。如果我們?cè)陧?xiàng)目根目錄執(zhí)行程序 bin/cwd,以上程序返回的 binary 結(jié)果是 bin/cwd,即程序 cwd 的相對(duì)路徑,可以通過 filepath.Abs() 函數(shù)得到絕對(duì)路徑,最后通過調(diào)用兩次 filepath.Dir 得到項(xiàng)目根目錄。

  1. binary, _ := exec.LookPath(os.Args[0]) 
  2.  
  3. root := filepath.Dir(filepath.Dir(filepath.Abs(binary))) 

03 使用 os.Executable

可能是類似的需求很常見,Go 在 1.8 專門為這樣的需求增加了一個(gè)函數(shù):

  1. // Executable returns the path name for the executable that started the current process. 
  2. // There is no guarantee that the path is still pointing to the correct executable. 
  3. // 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
  4. // If a stable result is needed, path/filepath.EvalSymlinks might help. 
  5. // Executable returns an absolute path unless an error occurred. 
  6. // The main use case is finding resources located relative to an executable. 
  7. func Executable() (string, error) 

和 exec.LookPath 類似,不過該函數(shù)返回的結(jié)果是絕對(duì)路徑。因此,不需要經(jīng)過 filepath.Abs 處理。

  1. binary, _ := os.Executable() 
  2. root := filepath.Dir(filepath.Dir(binary)) 

注意,exec.LookPath 和 os.Executable 的結(jié)果都是可執(zhí)行程序的路徑,包括可執(zhí)行程序本身,比如 /Users/xuxinhua/stdcwd/bin/cwd

細(xì)心的讀者可能會(huì)注意到該函數(shù)注釋中提到符號(hào)鏈接問題,為了獲得穩(wěn)定的結(jié)果,我們應(yīng)該借助 filepath.EvalSymlinks 進(jìn)行處理。

  1. package main 
  2.  
  3. import ( 
  4.     "fmt" 
  5.     "os" 
  6.     "path/filepath" 
  7.  
  8. func main() { 
  9.     ex, err := os.Executable() 
  10.     if err != nil { 
  11.         panic(err) 
  12.     } 
  13.     exPath := filepath.Dir(ex) 
  14.     realPath, err := filepath.EvalSymlinks(exPath) 
  15.     if err != nil { 
  16.         panic(err) 
  17.     } 
  18.     fmt.Println(filepath.Dir(realPath)) 

最后輸出的就是項(xiàng)目根目錄。(如果你的可執(zhí)行文件放在根目錄下,最后的 filepath.Dir 就不需要了)

注意:exec.LookPath 也有軟鏈接的問題。

exec.LookPath 和 os.Executable 函數(shù),再提醒一點(diǎn),如果使用 go run 方式運(yùn)行,結(jié)果會(huì)是臨時(shí)文件。因此,記得先編譯(這也是比 go run 更好的方式,go run 應(yīng)該只是用來本地測(cè)試)。

04 小結(jié)

 

既然 Go1.8 為我們專門提供了這樣一個(gè)函數(shù),針對(duì)本文提到的場(chǎng)景,我們應(yīng)該總是使用它。

 

責(zé)任編輯:武曉燕 來源: polarisxu
相關(guān)推薦

2016-11-23 18:52:04

根目錄日志代碼

2016-11-21 21:28:51

根目錄日志代碼

2021-07-13 09:45:13

高并發(fā)項(xiàng)目經(jīng)驗(yàn)

2019-10-11 15:10:09

GVMGoLinux

2024-11-06 09:23:32

2010-04-09 10:08:22

Unix操作系統(tǒng)

2018-03-26 13:45:27

LinuxWeb服務(wù)器文件權(quán)限

2024-02-21 08:33:27

GoReadDir性能

2024-07-12 08:56:40

2016-12-19 09:02:05

Linux Shell刪除跑路

2022-01-10 09:39:49

Centos根目錄權(quán)限

2023-12-27 19:52:08

Go模塊命令

2022-05-16 10:58:12

Go 項(xiàng)目Makefilemake

2022-03-13 23:51:39

Web項(xiàng)目Go

2018-03-07 16:55:16

Linux命令刪除根目錄

2009-11-25 09:03:31

WordPressPHP

2023-12-29 07:04:28

Go項(xiàng)目Docker編寫

2025-02-14 08:56:09

GoroutineContextChannel

2019-09-16 16:21:38

Go語言編程語言Python

2010-05-28 08:52:18

SVN項(xiàng)目
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)