Go OS Package

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("FMT" "Io/ioutil" "OS" "Reflect" "Time"Func Main () {dir, _:=OS. GETWD () fmt. Println ("Dir:", dir) Err:= OS. Chdir ("D:/project/test2") dir, _=OS. GETWD () fmt. Println ("Dir:", dir)//parameter is case insensitive    //return empty string len (path) = 0 if no environment variable existsPath: = OS. Getenv ("Gopath") fmt. Println (PATH)//returns a valid group IDEgid: =OS. Getegid () fmt. Println ("Egid:", Egid)//return valid UIDEuid: =OS. Geteuid () fmt. Println ("Euid:", Euid) GID:=OS. Getgid () fmt. Println ("GID:", GID) UID:=OS. Getuid () fmt. Println ("UID:", UID)//Err:getgroups:not supported by WindowsG, err: =OS. GetGroups () fmt. Println (g,"Error", Err) pagesize:=OS. GetPageSize () fmt. Println ("PageSize:", pagesize) Ppid:=OS. Getppid () fmt. Println ("Ppid", Ppid)//FileMode, err: = OS. Stat ("Main.go")    //A file that does not exist returns GetFileAttributesEx Test2:the system cannot find the file specified.FileMode, err: = OS. Stat ("Main.go")    ifErr = =Nil {fmt. Println ("Filename:", FileMode. Name ()) fmt. Println ("Filesize:", FileMode. Size ()) fmt. Println ("Filemode:", FileMode. Mode ()) fmt. Println ("Modtime:", FileMode. Modtime ()) fmt. Println ("Is_dir", FileMode. Isdir ()) fmt. Println ("SYS", FileMode. Sys ())}Else{fmt. Println ("OS. Stat Error ", Err)} //Chmod is not supported under Windows.    //in Windows change is such a-rw-rw-rw-=-r--r--r--Err = OS. Chmod ("Main.go", 7777) fmt. Println ("Chmod:", err) FileMode, err= OS. Stat ("Main.go") fmt. Println ("Filemode:", FileMode. Mode ())//Access Time Modification timeErr = OS. Chtimes ("Main.go", time. Now (), time. Now ()) FMT. Println ("Chtime Error:", Err)//get all the environment variablesData: =OS. Environ () for_, Val: =range Data {fmt. Println (val)} fmt. Println ("---------End---environ----------------------") Mapping:=func (s string) string {m:= map[string]string{"xx": "Sssssssssssss",            "yy": "Ttttttttttttttt"}        returnM[s]} datas:= "Hello $xx blog Address $yy"//This function feels pretty useful .EXPANDSTR: =OS. Expand (datas, mapping) FMT. Println (EXPANDSTR) datas= "GOBIN PATH $gopaTh"// Case insensitiveFMT. Println (OS. Expandenv (datas)) hostname, ERR:=OS. Hostname () fmt. Println ("Hostname:", hostname) _, Err= OS. Open ("wwww.xx")    ifErr! =Nil {fmt. Println (OS. Isnotexist (Err)) fmt. PRINTLN (Err)} F, err:= OS. Open ("wwww.xx")    ifErr! = Nil &&!OS. Isexist (Err) {FMT. Println (F,"Not Exist")    }    //Two of all windows are trueFmt. Println (OS. Ispathseparator ('/') FMT. Println (OS. Ispathseparator ('\\') FMT. Println (OS. Ispathseparator ('.'))    //determine if the error returned is a permission issue    //func ispermission (err error) bool    //Not supported by WindowsErr = OS. Link ("Main.go", "Newmain.go")    ifErr! =Nil {fmt. PRINTLN (ERR)}varPathsep StringifOs. Ispathseparator (' \ \ ') {Pathsep= "\\"    } Else{pathsep= "/"} fmt. Println ("PathSeparator:", Pathsep)//Mkdirall creates all subordinate directories, and if not, create them.    //Mkdir Create a directory, if it is multi-level directory encountered has not been created will be an errorErr = OS. Mkdir (dir+pathsep+ "MD" +pathsep+ "MD" +pathsep+ "MD" +pathsep+ "MD" +pathsep+ "MD", OS. Modeperm)ifErr! =Nil {fmt. Println (OS. Isexist (ERR), err)} Err= OS. RemoveAll (dir + "MD\\MD\\MD\\MD\\MD")) fmt. Println ("Removall", Err)//Rename is actually implemented through MoveFile .Err = OS. Rename ("Main.go", "Main1.go") F1, _:= OS. Stat ("Main.go") F2, _:= OS. Stat ("Main1.go")    ifOS. Samefile (f1, F2) {fmt. Println ("The Sanme")    } Else{fmt. Println ("Not Same")    }    //OS. Setenv This function is very simple to set environment variablesEVN: = os. Getenv ("Wd_path") fmt. Println ("Wd_path:", EVN) Err= OS. Setenv ("Wd_path", "D:/project")    ifErr! =Nil {fmt. PRINTLN (ERR)} tmp, _:= Ioutil. TempDir (dir, "tmp") fmt. PRINTLN (TMP) TMP=OS. TempDir () fmt. PRINTLN (TMP) CF, err:= OS. Create ("Golang.go") Defer cf. Close () fmt. PRINTLN (Err) fmt. Println (reflect. ValueOf (f). Type ()) of, err:= OS. OpenFile ("Golang.goss", OS. O_rdwr|os. O_create|os. O_trunc, 0777) Defer of. Close () fmt. Println ("OS. OpenFile: ", err) oof, err:= OS. Open ("Golang.goss") defer oof. Close () fmt. Println ("OS. Open file: ", Oof. Fd ()) fmt. Println ("OS. Open ERR: ", err) oof. Close () R, W, err:=OS. Pipe () W.write ([]byte("1111"))    varBUF = make ([]byte, 4) R.read (BUF) fmt. Println (BUF) w.write ([]byte("2222")) R.read (BUF)//If you do not call W. Write (), R.read () will blockFmt. Println ("ssss--", buf) B:= Make ([]byte, 100) FF, _:= OS. Open ("Main.go") N, _:=ff. Read (b) fmt. PRINTLN (n) fmt. Println (String (B[:n]))//The second parameter, refers to, starts from the first readingN, _ = ff. ReadAt (b, 20) fmt. PRINTLN (n) fmt. Println (String (B[:n]))//get a list of files under a folderDirs, err: = OS. Open ("MD")    ifErr! =Nil {fmt. PRINTLN (ERR)} defer dirs. Close ()//parameter is less than or equal to 0, indicating that all files are read    //another function that only reads the file name    //FS, err: = dirs. Readdirname (0)FS, err: = dirs. Readdir (-1)    ifErr = =Nil { for_, File: =Range FS {fmt. Println (file. Name ())}}Else{fmt. Println ("Readdir:", Err)} //func (f *file) WriteString (S string) (ret int, err error)    //Write a String function prototype, which function is faster??     //P, _: = OS. Findprocess (628)    //FMT. PRINTLN (P)    //P.kill ()attr: = &OS. procattr{Files: []*OS. File{os. Stdin, OS. Stdout, OS. Stderr},}//parameters can also be written ' C:\windows\system32\notepad. EXE ' with inverted single quotesP, err: = OS. StartProcess ("C:\\windows\\system32\\notepad. EXE ", []string{" C:\\windows\\system32\\notepad. EXE "," D:/1.txt "}, attr) P.release () time. Sleep (1000000000) p.signal (OS. Kill) OS. Exit (10)}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.