網(wǎng)站首頁 編程語言 正文
導(dǎo)讀
由于Golang是編譯型語言(非腳本型語言),如果你想在Golang程序中獲取當(dāng)前執(zhí)行目錄將是一件非常蛋疼的事情。以前大家最折中的解決方案就是通過啟動(dòng)傳參或是環(huán)境變量將路徑手動(dòng)傳遞到程序,而今天我在看日志庫的時(shí)候發(fā)現(xiàn)了一種新的解決方案。
Go程序兩種不同的執(zhí)行方式
用Go編寫的程序有兩種執(zhí)行方式,go run和go build
通常的做法是go run用于本地開發(fā),用一個(gè)命令中快速測(cè)試代碼確實(shí)非常方便;在部署生產(chǎn)環(huán)境時(shí),我們會(huì)通過go build構(gòu)建出二進(jìn)制文件然后上傳到服務(wù)器再去執(zhí)行。
兩種啟動(dòng)方式會(huì)產(chǎn)生什么問題?
那么兩種啟動(dòng)方式下,獲取到當(dāng)前執(zhí)行路徑會(huì)產(chǎn)生什么問題?
話不多說,我們直接上代碼
我們編寫獲取當(dāng)前可執(zhí)行文件路徑的方法
package main import ( "fmt" "log" "os" "path/filepath" ) func main() { fmt.Println("getCurrentAbPathByExecutable = ", getCurrentAbPathByExecutable()) } // 獲取當(dāng)前執(zhí)行程序所在的絕對(duì)路徑 func getCurrentAbPathByExecutable() string { exePath, err := os.Executable() if err != nil { log.Fatal(err) res, _ := filepath.EvalSymlinks(filepath.Dir(exePath)) return res
首先通過go run啟動(dòng)
D:\Projects\demo>go run main.go
getCurrentAbPathByExecutable = C:\Users\XXX\AppData\Local\Temp\go-build216571510\b001\exe
再嘗試go build執(zhí)行
D:\Projects\demo>go build & demo.exe
getCurrentAbPathByExecutable = D:\Projects\demo
通過對(duì)比執(zhí)行結(jié)果,我們發(fā)現(xiàn)兩種執(zhí)行方式,我們獲取到了不同的路徑。而且很明顯,go run獲取到的路徑是錯(cuò)誤的。
原因: 這是由于go run會(huì)將源代碼編譯到系統(tǒng)TEMP或TMP環(huán)境變量目錄中并啟動(dòng)執(zhí)行;而go build只會(huì)在當(dāng)前目錄編譯出可執(zhí)行文件,并不會(huì)自動(dòng)執(zhí)行。
我們可以簡(jiǎn)單理解為,go run main.go等價(jià)于go build & ./main
雖然兩種執(zhí)行方式最終都是一樣的過程:源碼->編譯->可執(zhí)行文件->執(zhí)行輸出,但他們的執(zhí)行目錄卻完全不一樣了。
新的方案誕生
這是在我今天查看服務(wù)日志(zap庫)的時(shí)候,突然反應(yīng)過來一件事情。比如下面是一條簡(jiǎn)單的日志,而服務(wù)是通過go run啟動(dòng)的,但日志庫卻把我正確的程序路徑D:/Projects/te-server/modules/es/es.go:139給打印出來了
2021-03-26 17:47:06 D:/Projects/te-server/modules/es/es.go:139 update es index {"index": "tags", "data": "[200 OK] {"acknowledged":true}"}
于是我馬上去翻看zap源碼,發(fā)現(xiàn)是通過runtime.Caller()實(shí)現(xiàn)的,其實(shí)所有Golang日志庫都會(huì)有runtime.Caller()這個(gè)調(diào)用。
我開心的以為找到了最終答案,然后寫代碼試了下:
package main import ( "fmt" "path" "runtime" ) func main() { fmt.Println("getCurrentAbPathByCaller = ", getCurrentAbPathByCaller()) } // 獲取當(dāng)前執(zhí)行文件絕對(duì)路徑(go run) func getCurrentAbPathByCaller() string { var abPath string _, filename, _, ok := runtime.Caller(0) if ok { abPath = path.Dir(filename) return abPath
首先在windows下面go run 和go build試一下
D:\Projects\demo>go run main.go
getCurrentAbPathByCaller = D:/Projects/demo
D:\Projects\demo>go build & demo.exe
getCurrentAbPathByCaller = D:/Projects/demo
嗯~~ 結(jié)果完全正確!
然后我再把構(gòu)建好的程序扔到linux再運(yùn)行后,它把我windows的路徑給打印出來了 --!
[root@server app]# chmod +x demo
[root@server app]# ./demo
getCurrentAbPathByCaller = D:/Projects/demo
沒想到白白高興一場(chǎng),這個(gè)時(shí)候我就在想,既然go run時(shí)可以通過runtime.Caller()獲取到正確的結(jié)果,go build時(shí)也可以通過 os.Executable()來獲取到正確的路徑;
那如果我能判定當(dāng)前程序是通過go run還是go build執(zhí)行的,選擇不同的路徑獲取方法,所有問題不就迎刃而解了嗎。
區(qū)分程序是go run還是go build執(zhí)行
Go沒有提供接口讓我們區(qū)分程序是go run還是go build執(zhí)行,但我們可以換個(gè)思路來實(shí)現(xiàn):
根據(jù)go run的執(zhí)行原理,我們得知它會(huì)源代碼編譯到系統(tǒng)TEMP或TMP環(huán)境變量目錄中并啟動(dòng)執(zhí)行;
那我們可以直接在程序中對(duì)比os.Executable()獲取到的路徑是否與環(huán)境變量TEMP設(shè)置的路徑相同, 如果相同,說明是通過go run啟動(dòng)的,因?yàn)楫?dāng)前執(zhí)行路徑是在TEMP目錄;不同的話自然是go build的啟動(dòng)方式。
下面是完整代碼:
package main import ( "fmt" "log" "os" "path" "path/filepath" "runtime" "strings" ) func main() { fmt.Println("getTmpDir(當(dāng)前系統(tǒng)臨時(shí)目錄) = ", getTmpDir()) fmt.Println("getCurrentAbPathByExecutable(僅支持go build) = ", getCurrentAbPathByExecutable()) fmt.Println("getCurrentAbPathByCaller(僅支持go run) = ", getCurrentAbPathByCaller()) fmt.Println("getCurrentAbPath(最終方案-全兼容) = ", getCurrentAbPath()) } // 最終方案-全兼容 func getCurrentAbPath() string { dir := getCurrentAbPathByExecutable() if strings.Contains(dir,getTmpDir()) { return getCurrentAbPathByCaller() return dir // 獲取系統(tǒng)臨時(shí)目錄,兼容go run func getTmpDir() string { dir := os.Getenv("TEMP") if dir == "" { dir = os.Getenv("TMP") res, _ := filepath.EvalSymlinks(dir) return res // 獲取當(dāng)前執(zhí)行文件絕對(duì)路徑 func getCurrentAbPathByExecutable() string { exePath, err := os.Executable() if err != nil { log.Fatal(err) res, _ := filepath.EvalSymlinks(filepath.Dir(exePath)) // 獲取當(dāng)前執(zhí)行文件絕對(duì)路徑(go run) func getCurrentAbPathByCaller() string { var abPath string _, filename, _, ok := runtime.Caller(0) if ok { abPath = path.Dir(filename) return abPath
在windows執(zhí)行
D:\Projects\demo>go run main.go
getTmpDir(當(dāng)前系統(tǒng)臨時(shí)目錄) = C:\Users\XXX\AppData\Local\Temp
getCurrentAbPathByExecutable(僅支持go build) = C:\Users\XXX\AppData\Local\Temp\go-build456189690\b001\exe
getCurrentAbPathByCaller(僅支持go run) = D:/Projects/demo
getCurrentAbPath(最終方案-全兼容) = D:/Projects/demo
D:\Projects\demo>go build & demo.exe
getTmpDir(當(dāng)前系統(tǒng)臨時(shí)目錄) = C:\Users\XXX\AppData\Local\Temp
getCurrentAbPathByExecutable(僅支持go build) = D:\Projects\demo
getCurrentAbPathByCaller(僅支持go run) = D:/Projects/demo
getCurrentAbPath(最終方案-全兼容) = D:\Projects\demo
在windows編譯后上傳到Linux執(zhí)行
[root@server app]# pwd
/data/app
[root@server app]# ./demo
getTmpDir(當(dāng)前系統(tǒng)臨時(shí)目錄) = .
getCurrentAbPathByExecutable(僅支持go build) = /data/app
getCurrentAbPathByCaller(僅支持go run) = D:/Projects/demo
getCurrentAbPath(最終方案-全兼容) = /data/app
對(duì)比結(jié)果,我們可以看到,在不同的系統(tǒng)中,不同的執(zhí)行方式,我們封裝的getCurrentAbPath方法最終都輸出的正確的結(jié)果,perfect!
原文鏈接:https://www.cnblogs.com/jiftle/p/16177738.html
相關(guān)推薦
- 2022-01-19 解決element-ui 表格分頁序號(hào)不遞增問題。
- 2022-09-12 python?通過dict(zip)和{}的方式構(gòu)造字典的方法_python
- 2023-03-27 React?中的重新渲染實(shí)現(xiàn)_React
- 2021-12-02 Centos8搭建配置nis域服務(wù)詳細(xì)步驟_Linux
- 2023-04-07 C#中將dateTimePicker初始值設(shè)置為空_C#教程
- 2024-03-24 required a single bean, but 2 were found
- 2022-07-12 Linux中xargs命令的用法
- 2022-04-28 sql?server?累計(jì)求和實(shí)現(xiàn)代碼_MsSql
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支