Golang (go language) standard Library analysis OS (8)

Source: Internet
Author: User
Tags lstat
This is a creation in Article, where the information may have evolved or changed.





Golang Standard Library



Today we continue the OS package, not much to say a word is Golang OS package





(1) Type FileInfo inside two functions stat and lstat I'm not going to say much, using the same method. We also introduced
[PHP]
Import (
"FMT"
"OS"
)



Func Main () {
It's about the stat.
F, _: = OS. Lstat ("1.go")
Fmt. Println (f)//&{1.go {2883792444 30345044} {2884358853 30345044} {3464863792 30345293} 0} {0 0} D:\test\1.go 0 0 0}
}



Type FileMode
Func (M FileMode) isdir () bool
Func (M FileMode) isregular () bool
Func (M FileMode) Perm () FileMode
Func (M FileMode) string () string



[/php]



(2) This filemode is mainly used for judging and exporting permissions, this is not much to say, so I'll just give you a test of the code



[PHP]
Import (
"FMT"
"OS"
"Reflect"
)



Func Main () {
F, _: = OS. Stat ("1.go")
Fi: = F.mode ()
Fmt. Println (reflect. TypeOf (FI))//os. FileMode
Fmt. Println (FI. Isdir ())//false determine if it is a directory
Fmt. Println (FI. Isregular ())//true determine if it is a regular file
Fmt. Println (FI. Perm ())//-rw-rw-rw-returns the Unix plain
Fmt. Println (FI. String ())//-rw-rw-rw-returns a string form of the file pattern
}



[/php]



(3) func (e *linkerror) Error () string This function is to get the Linkerror string form, first we look at the structure



[PHP]
Type Linkerror struct {
Op string
Old string
New string
ERR Error
}



Import (
"Errors"
"FMT"
"OS"
)



Func Main () {
Linkerror: = &os. linkerror{
Op: "Widuu",
Old: ' Old ',
NEW: "New Error",
Err:errors. New ("Error Test"),
}
Fmt. Println (Linkerror. Error ())//widuu old New Error:error test
}



[/php]



(4) func (e *patherror) Error () string This function is to get the Patherror string form, and then we look at its structure



[PHP]
Type Patherror struct {
Op string
Path string
ERR Error
}
Import (
"Errors"
"FMT"
"OS"
)



Func Main () {
Patherr: = &os. patherror{
Op: "dir",
Path: "Widuu",
Err:errors. New ("No Path"),
}
Fmt. Println (Patherr. Error ())//dir Widuu:no Path
}



[/php]



(5) Type process we are talking about the method under this structure



[PHP]
Type Process struct {
Pid int
}
[/php]



[1] Findprocess () function prototype func findprocess (PID int) (P *process, err Error) The PID of the input process returns the pointer structure of the process and the error information



[PHP]
Import (
"FMT"
"OS"
)



Func Main () {
P, _: = OS. Findprocess (6816)//Now cmd under my native windows
Fmt. PRINTLN (p)//&{6816 236 0}
}
[/php]



[2]os. StartProcess () function prototype func startprocess (name string, argv []string, attr *procattr) (*process, error) function is a new process, You must enter a name and parameter to specify the environment, this is a low-level interface, when we say os/exec there are more advanced interfaces, the return is the structure of the process and Patherror



[PHP]
Import (
"FMT"
"OS"
)



Func Main () {
attr: = &os. procattr{
Files: []*os. File{os. Stdin, OS. Stdout, OS. Stderr},//note here ha
}
P, err: = OS. StartProcess (' C:\windows\system32\notepad. EXE ', []string{' C:\windows\system32\notepad. EXE ', ' d:/1.txt '}, attr)//windows open folder under 1.txt
If err! = Nil {
Fmt. Println (Err. Error ())
}
Fmt. PRINTLN (p)//&{5488 240 0}
}



[/php]



[3]func (P *process) Kill () error A look we know is the kill process, we tested



[PHP]
Import (
"FMT"
"OS"
)



Func Main () {
P, _: = OS. Findprocess (7588)//Now cmd under my native windows
Fmt. PRINTLN (p)//&{7588 224 0}
ERR: = P.kill ()
If err! = Nil {
Fmt. PRINTLN (ERR)
}
Fmt. PRINTLN ("Kill process")
}



Very depressed thing is ~ ~ ~ I am using this process godoc-http:=8080 still stay again * * *
[/php]



[4]func (P *process) release () error This is the resource for the release process. and take the experiment.



[PHP]
Import (
"FMT"
"OS"
)



Func Main () {
attr: = &os. procattr{
Files: []*os. File{os. Stdin, OS. Stdout, OS. Stderr},
}
P, err: = OS. StartProcess (' C:\windows\system32\notepad. EXE ', []string{' C:\windows\system32\notepad. EXE ', ' d:/1.txt '}, attr)//windows open folder under 1.txt
If err! = Nil {
Fmt. Println (Err. Error ())//&{7704 244 0}
}
Fmt. PRINTLN (P)
If err: = P.release (); Err! = Nil {
Fmt. Printf ("Error:%v\n", err)
}
}



[/php]



[5]func (P *process) Signal (sig Signal) Error This is for sending a signal to a process.



[PHP]
Import (
"FMT"
"OS"
)



Func Main () {
attr: = &os. procattr{
Files: []*os. File{os. Stdin, OS. Stdout, OS. Stderr},
}
P, _: = OS. StartProcess (' C:\windows\system32\notepad. EXE ', []string{' C:\windows\system32\notepad. EXE ', ' d:/1.txt '}, attr)
Fmt. PRINTLN (P)
If err: = p.signal (OS. Kill); Err! = Nil {//Send a signal to the system to kill him so our familiar notepad won't come out.
Fmt. PRINTLN (ERR)
}
}



[/php]



[6]func (P *process) Wait () (*processstate, error) as soon as you see, this function is waiting for the function of the process to complete, and then return the process struct body pointer to the



[PHP]
Import (
"FMT"
"OS"
)



Func Main () {
attr: = &os. procattr{
Files: []*os. File{os. Stdin, OS. Stdout, OS. Stderr},
}
P, _: = OS. StartProcess (' C:\windows\system32\notepad. EXE ', []string{' C:\windows\system32\notepad. EXE ', ' d:/1.txt '}, attr)
PW, _: = P.wait ()
Fmt. Println (PW)//exit status 0
}



[/php]



(6) We tell the structure of type processstate struct {} and the method inside



[1]func (P *processstate) Exited () bool to see if the process exited



[PHP]
Import (
"FMT"
"OS"
)



Func Main () {
attr: = &os. procattr{
Files: []*os. File{os. Stdin, OS. Stdout, OS. Stderr},
}
P, _: = OS. StartProcess (' C:\windows\system32\notepad. EXE ', []string{' C:\windows\system32\notepad. EXE ', ' d:/1.txt '}, attr)
Fmt. PRINTLN (p)//&{7764 252 0}
PW, _: = P.wait ()
Fmt. Println (PW. Exited ())//When I close Notepad, the process ends, so even true
}
[/php]

[2] func (p *ProcessState) Pid () int very simple to return the pid of the process, the rest I will not say one by one, then directly explain and then do the experiment



[3] func (p *ProcessState) String() string This function is mainly a string to get the state of the process.

[4]func (p *ProcessState) Success() bool This function reports whether to exit

[5] func (p *ProcessState) Sys () interface{} This function is mainly to get the exit information of the process

[6] func (p *ProcessState) SysUsage () interface{} This function is mainly to obtain process resource usage

[7] func (p *ProcessState) SystemTime() time.Duration This function is mainly used to obtain the system cpu usage time of the process.



[8] func (p *ProcessState) UserTime() time.Duration This function is mainly used to get the user cpu usage time of the process.



[PHP]
Import (
"FMT"
"OS"
)



Func Main () {
attr: = &os. procattr{
Files: []*os. File{os. Stdin, OS. Stdout, OS. Stderr},
}
P, _: = OS. StartProcess (' C:\windows\system32\notepad. EXE ', []string{' C:\windows\system32\notepad. EXE ', ' d:/1.txt '}, attr)
Fmt. PRINTLN (p)//&{44 252 0}
PW, _: = P.wait ()
Fmt. Println (PW. Pid ())//44
Fmt. Println (PW. String ())//exit status 0
Fmt. Println (PW. Success ())//true
Fmt. Println (PW. Sys ())//{0}
Fmt. Println (PW. Sysusage ())//&{{1728533532 30345488} {1745163891 30345488} {1562500 0} {1562500 0}}
Fmt. Println (PW. SystemTime ())//156.25ms
Fmt. Println (PW. Usertime ())//156.25ms
}



[/php]



(7) func (e *syscallerror) Error () string This function is mainly to get syscallerror string form



[PHP]
Type Syscallerror struct {
Syscall string
ERR Error
}
Import (
"Errors"
"FMT"
"OS"
)



Func Main () {
ERR: = &os. syscallerror{
Syscall: "Widuu",
Err:errors. New ("Syscall error"),
}
Fmt. Println (Err. Error ())//widuu:syscall error
}
[/php]



Today's OS package is over, tomorrow we continue to talk about the OS sub-package, then thank you for your attention to my blog [Micro Network | Network Technology Support Center] (http://www.widuu.com) http://www.widuu.com***



Without permission, do not reprint this site any article: Micro network»golang (go Language) standard library analysis of the OS (8)


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.