<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Go語言:如何解決讀取不到相對路徑配置文件問題

      背景

      項目交叉編譯為可執行文件之后,在其他目錄執行文件時提示找不到配置文件

      2020/03/14 20:44:23 配置文件讀取失敗 open config.ini: no such file or directory
      

      解決方案

      直接采用以下代碼獲取到實際執行文件的路徑,然后拼接配置文件即可

      file, _ := exec.LookPath(os.Args[0])
      path, _ := filepath.Abs(file)
      index := strings.LastIndex(path, string(os.PathSeparator))
      path = path[:index]
      

      代碼分析

      os.Args是用來獲取命令行執行參數分片的,當使用go run

      $ go run main.go
      [/var/folders/3s/5v6r481x17x5ks_7q1dzmlsw0000gp/T/go-build231739964/b001/exe/main]
      

      分片0會是一串復雜的路徑,原因是直接run go文件時會將文件轉移到臨時路徑下,然后再進行編譯和執行,如果直接執行編譯后的文件就不一樣了,此時分片0為執行文件的相對路徑

      $ go build
      $ ./jira_reminder
      [./jira-reminder]
      

      接下來看一下LookPath方法的作用,官方文檔中是這樣解釋的

      // LookPath searches for an executable named file in the
      // directories named by the PATH environment variable.
      // If file contains a slash, it is tried directly and the PATH is not consulted.
      // The result may be an absolute path or a path relative to the current directory.
      

      大致意思就是它會去環境變量中找這個可執行文件的絕對路徑,或相對于當前目錄的路徑。接下來執行了filepath.Abs方法

      // Abs returns an absolute representation of path.
      // If the path is not absolute it will be joined with the current
      // working directory to turn it into an absolute path. The absolute
      // path name for a given file is not guaranteed to be unique.
      // Abs calls Clean on the result.
      

      意思是它會根據傳入的路徑計算出絕對路徑,如果傳入的為相對路徑,那么它會把當前路徑拼接上

      此時返回的path是一個包含可執行文件在內的完整路徑,我們只需要精確到目錄即可

      index := strings.LastIndex(path, string(os.PathSeparator))
      

      以上代碼會搜索最后一個目錄分隔符的位置(下標),然后通過以下代碼將路徑中下標后面的字符串切割掉

      path = path[:index]
      

      這樣就完成了目錄的獲取,接下來再拼接上我們實際的配置文件就可以了

      番外

      發現不調用exec.LookPath也是可以達到查詢絕對路徑的目的的,那么exec.LookPath還有什么用?

      path, _ := filepath.Abs(os.Args[0])
      index := strings.LastIndex(path, string(os.PathSeparator))
      path = path[:index]
      

      我們來看一下源碼,exec.LookPath的作用是從相對路徑或環境變量PATH中遞歸找可執行文件,這起著一個校驗的作用,檢測調用的可執行文件是不是真的存在,如果存在再繼續往下拼接出絕對路徑,因為我們的執行文件的確是存在的,所以就算不使用exec.LookPath也可以達到目的

      func LookPath(file string) (string, error) {
      	// NOTE(rsc): I wish we could use the Plan 9 behavior here
      	// (only bypass the path if file begins with / or ./ or ../)
      	// but that would not match all the Unix shells.
      
      	if strings.Contains(file, "/") {
      		err := findExecutable(file)
      		if err == nil {
      			return file, nil
      		}
      		return "", &Error{file, err}
      	}
      	path := os.Getenv("PATH")
      	for _, dir := range filepath.SplitList(path) {
      		if dir == "" {
      			// Unix shell semantics: path element "" means "."
      			dir = "."
      		}
      		path := filepath.Join(dir, file)
      		if err := findExecutable(path); err == nil {
      			return path, nil
      		}
      	}
      	return "", &Error{file, ErrNotFound}
      }
      
      posted @ 2020-03-14 23:51  MARIOOW  閱讀(8763)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 大香蕉av一区二区三区| 亚洲精品成人区在线观看| 国产亚洲一区二区三区啪| 国产成人精品日本亚洲专区6| 日本成人午夜一区二区三区| 一区二区不卡99精品日韩| 国产成人精品无码播放| 伊人欧美在线| 国产成人精品18| 国产AV巨作丝袜秘书| 精品人妻av区乱码| 午夜福利啪啪片| 国产福利视频区一区二区| 国产午夜福利视频在线| 成年美女黄网站色大片免费看| 欧美熟妇乱子伦XX视频| 精品国产乱码久久久久久婷婷| 日韩中文字幕av有码| 久热这里只有精品视频六| 最新的精品亚洲一区二区| 啊灬啊灬啊灬快灬高潮了电影片段 | 91久久久久无码精品露脸| 国产精品自在线拍国产手机版| 一区二区三区AV波多野结衣| 视频一区二区三区四区不卡| 国产精品久久久久7777| 狠狠爱俺也去去就色| 暖暖影院日本高清...免费| 日韩精品不卡一区二区三区| 国产亚洲天堂另类综合| 亚洲熟妇自偷自拍另欧美| 国产一区二区av天堂热| 中文日韩在线一区二区| 久久国产精品无码网站| 日韩av中文字幕有码| 成人亚洲欧美一区二区三区| 8av国产精品爽爽ⅴa在线观看| 亚洲精品久久麻豆蜜桃| 欧美熟妇xxxxx欧美老妇不卡| 欧美午夜精品久久久久久浪潮| 国产成人永久免费av在线|