Preliminary induction of common functions in the Go language's os package
- 2020-05-30 20:22:41
- OfStack
(1) os.Getwd function prototype is func Getwd() (pwd string, err error) returns the string of the path and an err message. Why open this first? Because when I look at the os package the first one is the Chkdir package, but you don't know the current directory how do you know if you've changed the directory? So Getwd(), demo
import (
"fmt"
"os"
)
func main() {
dir, _ := os.Getwd()
fmt.Println(" The current directory is :", dir) // The current directory is : D:\test My environment is windows if linix is /xxx/xxx
}
(2) having said Getwd(), let's say all the Get in os. os.Getenv () gets the environment variable of the system. The function prototype is func Getenv(key string). string enters the name of an string environment variable and returns the value
import (
"fmt"
"os"
)
func main() {
path := os.Getenv("GOPATH")
fmt.Println(" The environment variable GOPATH The value is :", path) //windows Under the The environment variable PATH The value is : D:\test;C:\Go\bin; linux The environment variable GOPATH The value is : /data/goweb
}
(3) if there is no: = in the get information below, it will return something rarely used like int1. What should I do for the annotation? And then windows and linux what's the result?
fmt.Println (os.Getegid ()) windows-1 linux 0 // the caller's group id
fmt.Println (os.Geteuid ()) windows-1 linux 0 // uid for user
fmt.Println (os.Getgid ()) windows-1 linux 0 // the caller's gid id
g, _ := os.Getgroups()
fmt.Println (g) windows [] linux [] // returns a slice of []int showing that the caller belongs to a group of 1 series id
fmt.Println (os.Getpagesize ()) windows 4096linux 4096 //windows is called virtual memory and linux is called swap
fmt.Println (os.Getppid ())) windows-1 linux 8621 // the caller's group process id
fmt.Println (os.Getuid ()) windows-1 linux 0 // the caller's digital user id
(4) os.Chdir () the prototype of this function is func Chdir(dir string) error input character type, return the error result, if the change is successful error=nil
import (
"fmt"
"os"
)
func main() {
fmt.Println(os.Getwd()) // Displays the current directory D:\test <nil>
fmt.Println(os.Chdir("D:/test/src")) // return <nil> Switch directories correctly
fmt.Println(os.Getwd()) // The changed directory D:\test\src <nil>
}
(5) os.Stat () this function is to get the information of the file. The prototype of the function func Stat(name string) (fi FileInfo, err error) output is the name of the file and returns the interface and err information of one FileInfo. We introduced the interface type of FileInfo in the last analysis of ioutil
type FileInfo interface {
Name() string // Name of file
Size() int64 // Sung file size
Mode() FileMode // File permissions
ModTime() time.Time // time
IsDir() bool // Is it 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()) // Access permissions linux 0600
}
(6) os.Chmod () the prototype of this function is func Chmod(name string, mode FileMode) error changes the properties of the file such as read and write, linux 0755 so you can understand
import (
"fmt"
"os"
)
func main() {
filemode, _ := os.Stat("widuu.go")
fmt.Println(filemode.Mode()) // Access permissions linux 0600
err := os.Chmod("widuu.go", 0777) // What has changed is the permissions of the file
if err!=nil{
fmt.Println(" Failed to modify file permissions ")
}
filemode, _ = os.Stat("widuu.go")
fmt.Println(filemode.Mode()) // Access is 0777
}
(7) os.Chtime () this package, the function is func Chtimes(name string, atime time.Time, mtime time.Time) error (name string, atime time.Time, mtime time.Time
import (
"fmt"
"os"
"time"
)
func main() {
err := os.Chtimes("2.go", time.Now(), time.Now()) // Change the time
if err != nil {
fmt.Println(err)
}
fi, _ := os.Stat("2.go")
fmt.Println(fi.ModTime()) // The output of time 2013-12-29 20:46:23.0005257 +0800 +0800
}
(8) os.Environ () is used to get the environment variable of the system, and the function is func Environ() []string returns the []string slice of the environment variable, which is explained together with the other 1, that is, os.ClearEnv () empties the environment variable
func main() {
data := os.Environ() // Output the previous environment variables 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) // The output []string Type of slice []
}
(9) os.Exit () is the code that interrupts the program to return a custom int type. The function runs with func Exit(code int) and enters a value of int
import (
"fmt"
"os"
)
func main() {
func() {
for {
fmt.Println(" This is an anonymous function ")
os.Exit(1) // The output exit status 1 Interrupt operation
}
}()
}
(10) function os.Expand () this is actually a callback function replacement method, the original function is func Expand(s string, mapping func(string) string) string input is 1 string. This corresponds to func(string), string's method of replacing a string with nothing if there are no characters
import (
"fmt"
"os"
)
func main() {
mapping := func(s string) string {
m := map[string]string{"widuu": "www.ofstack.com", "xiaowei": "widuu"}
return m[s]
}
data := "hello $xiaowei blog address $widuu"
fmt.Printf("%s", os.Expand(data, mapping)) // The output hello widuu blog address www.ofstack.com}
(11) os.ExpandEnv () replaces the s of the string with the contents of the environment variable. The original form of the function is func ExpandEnv(s string) string. The input is of course the character to be replaced, while the output is of course a string
import (
"fmt"
"os"
)
func main() {
data := "GOBIN PATH $GOBIN"
fmt.Println(os.ExpandEnv(data)) // The output of my local environment variable GOBIN The address of the GOBIN PATH C:\Go\bin
}
(12) os.Hostname () this function literally returns HostName() to the host, func Hostname() (name string, err error) returns the host name and the interface information of an error
import (
"fmt"
"os"
)
func main() {
path := os.Getenv("GOPATH")
fmt.Println(" The environment variable GOPATH The value is :", path) //windows Under the The environment variable PATH The value is : D:\test;C:\Go\bin; linux The environment variable GOPATH The value is : /data/goweb
}
0