This is a creation in Article, where the information may have evolved or changed.
The following are some common operations golang, get environment variables, operation files and so on;
The package main import ("FMT" "OS" "Path/filepath" "Io/ioutil") func main () {//connects the current path and the given path to an absolute path abspath,_: = FilePath. Abs ("Test") fmt. PRINTLN (Abspath)//Returns the last path (usually a directory name or file name) BasePath: = FilePath. Base (Abspath) fmt. PRINTLN (BasePath)//Returns a path other than the last directory (that is, the path before the last directory) Dirpath: = FilePath. Dir (Abspath) fmt. PRINTLN (Dirpath)//Returns the file suffix name (for example,. txt) in the path with the file name: = "C:\\1.txt" FileName: = filepath. EXT (file) fmt. PRINTLN (fileName)//replace '/' in the path with ' \ ', multiple '/' is replaced by multiple ' \ \ ' slash: = FilePath. Fromslash ("D://goproject//src//example//test") fmt. Println (slash)//The connection path becomes a full path joinpath: = FilePath. Join ("C:\\go\\test", "example") fmt. PRINTLN (Joinpath)//Output Execution program name (Test_filepath.exe) fmt. PRINTLN (filepath. Base (OS. ARGS[0])//Output Execution program information FMT. Println (OS. Stat (OS. ARGS[0])//Get subdirectories under a directory List,err: = Ioutil. ReadDir ("D:\\goproject\\src\\example") if err! = Nil {fmt. PRINTLN (Err)}for _, Item: = Range List {if item. Isdir () {fmt. PRINTLN (item. Name ())}}//determines whether the running user of the current process is rootfmt. Println (OS. Geteuid ())//Get environment variable FMT. Println (OS. Getenv ("Java_home"))}
Summary: Golang Basic operation is more, such as file operation, very detailed, please refer to the Golang API for detailed content. api:https://golang.org/doc/