7.1. Obtaining Operating System User Information
(1) os package and subpackage function
- os/exec package, responsible for executing external commands
- Access to input information by os/singal
- os/user queries user accounts by rank or ID
(2) User structure is provided in os/user to represent operating system users
- Uid user id
- Group id of Gid
- Username username
- Name Group Name
- HomeDir User Corresponding Folder Strength
(3) Group s in os/user represent groups to which users belong
- id of Gid group
- Name group name
//Learn_Go/main.go package main import ( "fmt" "os/user" ) func main() { u,error := user.Current() //Get the current user if error != nil{ fmt.Println(error) return } fmt.Println(u.Uid) fmt.Println(u.Name) fmt.Println(u.Gid) fmt.Println(u.HomeDir) fmt.Println(u.Username) }
7.2. System files and directories
(1) There are two ways to create folders in the Go Language Standard Library
MkDir: Requires that folders do not exist and that parent directories must exist in order to create
//Learn_Go/main.go package main import ( "fmt" "os" ) func main() { err := os.Mkdir("D:/godir", os.ModeDir) if err != nil{ fmt.Println("Folder creation failed", err) return } fmt.Println("Folder Creation Successful") }
MkDirAll: If the folder already exists, do not report errors, keep the original file, if the parent directory does not exist to help create
//Learn_Go/main.go package main import ( "fmt" "os" ) func main() { err := os.MkdirAll("D:/godir/a/b", os.ModeDir) if err != nil{ fmt.Println("Folder creation failed", err) return } fmt.Println("Folder Creation Successful") }
(2) Create empty files
Creating a file requires that the file directory already exists
If the file already exists, an empty file is created to overwrite the previous file.
//Learn_Go/main.go package main import ( "fmt" "os" ) func main() { f , err := os.Create("D:/godir/test.txt") if err != nil{ fmt.Println("Folder creation failed", err) return } fmt.Println("Folder Creation Successful", f) }
(3) Rename files or folders
First parameter: the name of the original file, requiring that this path must exist
Second parameter: new folder name
//Learn_Go/main.go package main import ( "fmt" "os" ) func main() { err := os.Rename("D:/godir","D:/godir11") if err != nil{ fmt.Println("Rename failed", err) return } fmt.Println("Successful renaming") }
Rename files
//Learn_Go/main.go package main import ( "fmt" "os" ) func main() { err := os.Rename("D:/godir11/test.txt","D:/godir11/test11.txt") if err != nil{ fmt.Println("Rename failed", err) return } fmt.Println("Successful renaming") }
(4) Getting folder information
//Learn_Go/main.go package main import ( "fmt" "os" ) func main() { f,err := os.Open("D:/godir11/test11.txt") if err != nil{ fmt.Println("File acquisition failure", err) return } fileInfo,err := f.Stat() if err != nil{ fmt.Println("Failure to obtain file information",err) return } fmt.Println(fileInfo.Size()) //file size fmt.Println(fileInfo.ModTime()) //Final Modification Time of Documents fmt.Println(fileInfo.Mode()) //File mode-rw-rw-rw- fmt.Println(fileInfo.IsDir()) //Is it a directory false? fmt.Println(fileInfo.Name()) //File name test11.txt }
(5) Delete files or folders
Remove: Deleted content can only be a file or empty folder and must exist
//Learn_Go/main.go package main import ( "fmt" "os" ) func main() { err := os.Remove("D:/godir11") if err != nil{ fmt.Println("Delete failed", err) return } fmt.Println("Successful deletion") } //Result //Delete failed remove D:/godir11: The directory is not empty.
RemoveAll
- Delete folders as long as they exist
- Documents will be deleted regardless of whether they have content or not.
- Delete files if they are files
//Learn_Go/main.go package main import ( "fmt" "os" ) func main() { err := os.RemoveAll("D:/godir11") if err != nil{ fmt.Println("Delete failed", err) return } fmt.Println("Successful deletion") } //Result //Successful deletion