1. 程式人生 > >1.5 獲取文件路徑

1.5 獲取文件路徑

gpo dir print file cnblogs != println AC private

package main

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

func main() {
    ex, err := os.Executable()
    if err != nil {
        panic(err)
    }

    // Path to executable file
    fmt.Println("絕對路徑: ", ex) // 絕對路徑

    // Resolve the direcotry
    // of the executable
    exPath := filepath.Dir(ex)
    fmt.Println("執行的路徑,文件所在目錄Executable path:"
+ exPath) // Use EvalSymlinks to get // the real path. realPath, err := filepath.EvalSymlinks(exPath) if err != nil { panic(err) } fmt.Println("Symlink evaluated:" + realPath) } /* 絕對路徑: /private/var/folders/tw/g0l_2m8s7y5690qqjdsvrzd00000gn/T/___go_build_b_go 執行的路徑,文件所在目錄Executable path:/private/var/folders/tw/g0l_2m8s7y5690qqjdsvrzd00000gn/T
Symlink evaluated:/private/var/folders/tw/g0l_2m8s7y5690qqjdsvrzd00000gn/T */

1.5 獲取文件路徑