Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of preliminary Induction of functions commonly used in os package in Go language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Go language os package commonly used function preliminary summary of the example analysis, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

(1) the prototype of the os.Getwd function is func Getwd () (pwd string, err error), which returns the string of the path and an err message. Why open this first? Because when I look at os's package, the first one is Chkdir, but you don't know how to change the current directory. So let's start with the Getwd () function demo.

The copy code is as follows:

Import (

"fmt"

"os"

)

Func main () {

Dir, _: = os.Getwd ()

Fmt.Println ("current directory is:", dir) / / the current directory is: d:\ test my environment is windows if linix is / xxx/xxx

}

(2) now that we have said Getwd (), let's talk about all the Get in os. Os.Getenv () gets the environment variable of the system. The function prototype is func Getenv (key string) string, which inputs the environment variable name of a string and returns the value.

The copy code is as follows:

Import (

"fmt"

"os"

)

Func main () {

Path: = os.Getenv ("GOPATH")

Fmt.Println ("the value of environment variable GOPATH is:", path) / / the value of environment variable PATH under windows is: d:\ test;C:\ Go\ bin; linux the value of environment variable GOPATH is: / data/goweb

}

(3) if the following get information is not: =, it is returned that int is rarely used. What do I do with the comments? Then what is the result of windows and linux?

Fmt.Println (os.Getegid ()) windows-1 linux 0 / / id of the caller's group

Fmt.Println (os.Geteuid ()) windows-1 linux 0 / / user's uid

Fmt.Println (os.Getgid ()) windows-1 linux 0 / / id of the caller's gid

G, _: = os.Getgroups ()

Fmt.Println (g) windows [] linux [] / returns a slice of [] int showing a series of id 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 linux 8621 / / process id of the caller's group

Fmt.Println (os.Getuid ()) windows-1 linux 0 / / Digital user id of the caller

(4) the prototype of the function os.Chdir () is the input character type of func Chdir (dir string) error, and the error result is returned. If the change is successful, error=nil

The copy code is as follows:

Import (

"fmt"

"os"

)

Func main () {

Fmt.Println (os.Getwd ()) / / displays the current directory D:\ test

Fmt.Println (os.Chdir ("D:/test/src")) / / returns the correct directory change

Fmt.Println (os.Getwd ()) / / switched directory D:\ test\ src

}

(5) os.Stat () this function is used to obtain the information of the file. The prototype func Stat (name string) (fi FileInfo, err error) output of the function returns the interface and err information of a FileInfo by the name of the file. When we analyzed ioutil last time, we introduced the interface type of FileInfo.

The copy code is as follows:

Type FileInfo interface {

Name of the Name () string / / file

Size () int64 / / sang the size of the file

Permissions for Mode () FileMode / / file

ModTime () time.Time / / time

Whether IsDir () bool / / is a directory

Sys () interface {} / / basic data Source Interface (can return nil)

}

Import (

"fmt"

"os"

)

Func main () {

Filemode, _: = os.Stat ("widuu.go")

Fmt.Println (filemode.Mode ()) / / get permission linux 0600

}

(6) the prototype of the os.Chmod () function is func Chmod (name string, mode FileMode) error to change the properties of a file, such as reading and writing, 0755 on linux, so you can understand it.

The copy code is as follows:

Import (

"fmt"

"os"

)

Func main () {

Filemode, _: = os.Stat ("widuu.go")

Fmt.Println (filemode.Mode ()) / / get permission linux 0600

Err: = os.Chmod ("widuu.go", 0777) / / changes the permissions of the file

If erratic roomnil {

Fmt.Println ("failed to modify file permissions")

}

Filemode, _ = os.Stat ("widuu.go")

Fmt.Println (filemode.Mode ()) / / get permission is 0777

}

(7) the package os.Chtime (). The prototype of the function is func Chtimes (name string, atime time.Time, mtime time.Time) error. The name of the file entered by string, access time, creation time, returns the error interface information.

The copy code is 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 purl 46 purl 23.0005257 + 0800 + 0800

}

(8) the function of os.Environ () is to obtain the environment variable of the system. The function prototype is func Environ () [] string returns a [] string slice of the environment variable. This should be explained together with others, that is, os.ClearEnv () clears the environment variable.

The copy code is as follows:

Func main () {

Data: = os.Environ () / / output the previous environment variable APPDATA=C:\ Users\ xiaolvge\ AppData\ Roaming CLASSPATH=.;D:\ java\ jdk1.6.0_38.

Fmt.Println (data)

Os.Clearenv () / / clear environment variables

Data = os.Environ ()

Fmt.Println (data) / / output [] slices of type string []

}

(9) os.Exit () is the code that interrupts the program to return a custom int type. The function runs as func Exit (code int) and enters a value of int.

The copy code is 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 replacement method, the prototype of the function is func Expand (s string, mapping func (string) string) string input is a string. Corresponding to the replacement string method of func (string) string, which is replaced with an empty string if there are no characters

The copy code is 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}

(11) os.ExpandEnv () replaces the s of the string with the content of the environment variable. The prototype of the function is func ExpandEnv (s string) string. Of course, the input is the character to be replaced, and of course the output is the string.

The copy code is as follows:

Import (

"fmt"

"os"

)

Func main () {

Data: = "GOBIN PATH $GOBIN"

Fmt.Println (os.ExpandEnv (data)) / / output the address of the GOBIN of my local environment variable GOBIN PATH C:\ Go\ bin

}

(12) the function os.Hostname () can be understood literally. It returns the HostName () of the host. The prototype of the function is func Hostname () (name string, err error), which returns the name of the host and the interface information of an error.

The copy code is as follows:

Import (

"fmt"

"os"

)

Func main () {

Data, _: = os.Hostname ()

Fmt.Println (data) / / I am the hostname WIDUU of the win returned to me in the windows environment

}

After reading the above, have you mastered the example analysis method of preliminary induction of functions commonly used in os packages in Go language? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report