A preliminary summary of common functions in the Go Language OS package _golang

Source: Internet
Author: User
Tags chmod function prototype key string file permissions

(1) OS. The GETWD function prototype is func GETWD () (PWD string, err Error) that returns the string of the path and a ERR message, why do you want to open this first? Because when I look at the OS package, the first one is Chkdir this package, but you do not know how the current directory to change the directory? So first say GETWD () function demo

Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
Dir, _: = OS. GETWD ()
Fmt. Println ("Current directory is:", dir)//current directory is: D:\test My environment is windows if Linix is/xxx/xxx
}


(2) since said GETWD (), we put the OS inside the get all said! Os. Getenv () Gets the system's environment variable, which is the Func Getenv (key String) string that enters the name of a string's environment variable and returns the value
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
Path: = OS. Getenv ("Gopath")
Fmt. PRINTLN ("Environment variable Gopath value is:", path)//windows The value of the environment variable path is: D:\test; C:\Go\bin; The value of the Linux environment variable Gopath is:/data/goweb
}


(3) The bottom of the Get information if not: = is the return of the int is generally rarely used, I gave a note of what to do? Then what are Windows and Linux results?
Fmt. Println (OS. Getegid ()) windows-1 the ID of the Linux 0//caller's group
Fmt. Println (OS. Geteuid ()) windows-1 The UID of the Linux 0//user
Fmt. Println (OS. Getgid ()) windows-1 Linux 0//ID of the caller's GID
G, _: = OS. GetGroups ()
Fmt. Println (g) Windows [] Linux []//Returns a []int slice showing a series of IDs that the caller belongs to the group
Fmt. Println (OS. GetPageSize ()) Windows 4096linux 4096//windows inside is called virtual memory Linux inside is called swap
Fmt. Println (OS. Getppid ()) windows-1 the process ID of the Linux 8621//caller's group
Fmt. Println (OS. Getuid ()) windows-1 Linux 0//caller's digital User ID
(4) OS. Chdir () The prototype of this function is the Func Chdir (dir string) error input character type that returns the error result if the change succeeds Error=nil
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
Fmt. Println (OS. GETWD ())//Display current directory D:\test <nil>
Fmt. Println (OS. Chdir ("D:/TEST/SRC"))//return <nil> correct switch directory
Fmt. Println (OS. GETWD ())//Toggle Directory D:\test\src <nil>
}


(5) OS. Stat () This function is to obtain information about the file, the function of the prototype Func Stat (name string) (Fi FileInfo, err error) output is the name of the file returns a FileInfo interface and err information, In the previous analysis Ioutil, we introduced the FileInfo interface type.
Copy Code code as follows:

Type FileInfo Interface {
Name () string//file names
Size () Int64//sing the file sizes
Mode () FileMode//file permissions
Modtime () time. Time//Times
Isdir () bool//is a directory
Sys () interface{}//Underlying data source interface (can return nil)
}
Import (
"FMT"
"OS"
)

Func Main () {
FileMode, _: = OS. Stat ("Widuu.go")
Fmt. Println (FileMode. Mode ())//Get Permissions Linux 0600
}


(6) OS. Chmod () The prototype of this function is func Chmod (name string, mode FileMode) error changing file properties such as read and write, Linux 0755 so you can understand.
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
FileMode, _: = OS. Stat ("Widuu.go")
Fmt. Println (FileMode. Mode ())//Get Permissions Linux 0600
ERR: = OS. Chmod ("Widuu.go", 0777)//Changing the permissions of the file
If err!=nil{
Fmt. PRINTLN ("Modify file permissions failed")
}
FileMode, _ = os. Stat ("Widuu.go")
Fmt. Println (FileMode. Mode ())//get permission is 0777

}


(7) OS. Chtime () This package, the prototype of the function is func chtimes (name string, Atime time. Time, Mtime time. Time) Error input string file name access time created to return error interface information
Copy Code code as follows:

Import (
"FMT"
"OS"
"Time"
)

Func Main () {
ERR: = OS. Chtimes ("2.go", time.) Now (), time. Now ())//Change Time
If Err!= nil {
Fmt. PRINTLN (ERR)
}
Fi, _: = OS. Stat ("2.go")
Fmt. Println (FI. Modtime ())//Output time 2013-12-29 20:46:23.0005257 +0800 +0800
}


(8) OS. The function of Environ () is to obtain the environment variable of the system, the function prototype is func Environ () []string return is the environment variable []string Slice, say this should be explained with the other, that is the OS. Clearenv () Empty environment variables
Copy Code code as follows:

Func Main () {
Data: = OS. Environ ()//output before the environment variable appdata=c:\users\xiaolvge\appdata\roaming classpath=.;D: \ Java\jdk1.6.0_38 .......
Fmt. PRINTLN (data)
Os. Clearenv ()//Empty environment variables
data = OS. Environ ()
Fmt. PRINTLN (data)//Output []string-type slices []
}

(9) OS. Exit () is the interrupt program returns the custom int type of code, function run is func Exit (code int) Enter the value of an int is OK
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
Func () {
for {
Fmt. Println ("This is an anonymous function")
Os. Exit (1)/Output exit Status 1 interrupt operation
}
}()
}


(10) Function OS. Expand () This is actually a callback function substitution method, the prototype of the function is Func Expand (s string, mapping Func (String) string) string entered a string. Corresponds to a method of Func (String) string substitution with null if there are no characters
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
Mapping: = Func (s string) string {
M: = map[string]string{"Widuu": "Www.jb51.net", "Xiaowei": "Widuu"}
return M[s]
}
Data: = "Hello $xiaowei blog Address $widuu"
Fmt. Printf ("%s", OS. Expand (data, mapping))//Output Hello Widuu blog address www.jb51.net}


(one) OS. EXPANDENV () replaces the string s with the contents of the environment variable, the prototype of the function is Func expandenv (s string) string, the input is of course the character to be replaced, the output is of course the string
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
Data: = "Gobin PATH $GOBIN"
Fmt. Println (OS. EXPANDENV (data)//output The Gobin address of my local environment variable Gobin PATH C:\Go\bin
}


(OS). Hostname () This function looks literal thinking to understand, is returns the host Hostname (), the function's prototype is Func Hostname () (name string, err Error) returns the host name and an error interface information
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
Data, _: = OS. Hostname ()
Fmt. PRINTLN (data)//I am the hostname that returns my win under Windows environment Widuu
}

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.