Operating Environment:
Golang1.4.2+win7x64
Golang1.4.2+centos6.5x64
1 Package Helper2 3 Import (4 "OS"5"Os/Exec "6"path/filepath "7 "Strings"8 ) 9 Ten /*gets the path of the current file execution*/ OneFunc Getcurpath ()string { AFile, _: = Exec. Lookpath (OS. args[0]) - - //get the full path, for example under Windows E:\\golang\\test\\a.exe thePath, _: =filepath. Abs (file) - - //divide the whole path with \ \, get 4 segments, ①e:②golang③test④a.exe -Splitstring: =strings. Split (path, "\ \") + - //size is 4 +Size: =Len (splitstring) A at //the entire path is segmented with the last segment (④a.exe), resulting in 2 segments, ①e:\\golang\\test\\②a.exe -Splitstring = strings. Split (Path, splitstring[size-1]) - - //replace \ \ in ① (e:\\golang\\test\\) with/, resulting in the result e:/golang/test/ -RST: = Strings. Replace (splitstring[0], "\ \", "/", size-1) - returnrst in}
Golang get the current path to the execution file