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

Source: Internet
Author: User
Tags key string

This is a creation in Article, where the information may have evolved or changed.








Golang Standard Library




Today we are analyzing the Golang OS package, why not analyze the IO package today and analyze the OS package? Check the code.io.WriteString(w Writer, s string)Where writer is the target writer, then we will findfunc (f *File) WriteString(s string) (ret int, err error)This, so I'm here to tell you about the OS package analysis! (not including child packages)








(1) OS. The GETWD function prototype is afunc Getwd() (pwd string, err error)string that returns a path and an ERR message, why do you open this first? Because I look at the OS package when the first one is Chkdir this package, but you do not know how the current directory to know how to change the directory? So first say GETWD () function demo
[PHP]
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 

[/php] 
(2) Now that you've said GETWD (), Let's just say all the get inside the OS! Os. Getenv () Gets the environment variable for the system, the function prototype isfunc Getenv (key string) stringinput is a string environment variable name, return is the value 
[PHP] 
Import ( 
FMT 
"OS" 
)





Func Main () {
Path: = OS. Getenv ("Gopath") 
FMT. Println (the value of the environment variable Gopath 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 

[/php] 
(3) below the Get information if not: = is the return is an int is seldom used I just give the comment what? Then what are the Windows and Linux results? 
[PHP] 
FMT. Println (OS. Getegid ()) windows-1 the ID of the group of Linux 0//caller 
FMT. Println (OS. Geteuid ()) windows-1 Linux 0//user uid 
FMT. Println (OS. Getgid ()) windows-1 Linux 0//caller's GID ID 
G, _: = OS. GetGroups () 
FMT. Println (g) Windows [] Linux []//Returns a []int slice showing the caller belongs to the group's series ID 
FMT. Println (OS. GetPageSize ()) Windows 4096linux 4096//windows inside called virtual memory Linux inside called Swap 
FMT. Println (OS. Getppid ()) windows-1 the process ID 
FMT for the group of Linux 8621//callers. Println (OS. Getuid ()) windows-1 the digital User ID of the Linux 0//Caller 
(4) OS. Chdir () The prototype of this function is the Func Chdir (dir string) error input character type, which returns an incorrect result if the change succeeds Error=nil 
Import (
"FMT" 
"OS" 
)





Func Main () {
Fmt. Println (OS. GETWD ())//Display the current directory D:\test <nil>
Fmt. Println (OS. Chdir ("D:/TEST/SRC"))//return <nil> correct directory switching
Fmt. Println (OS. GETWD ())//The directory after switching D:\test\src <nil>
}
[/php]
(5) OS. Stat () This function is to get the information of the file, the prototype output of the function function is the name of thefunc Stat(name string) (fi FileInfo, err error)file to return a FileInfo interface and err information, the last time we analyzed Ioutil, we introduced FileInfo interface type.
[PHP]
Type FileInfo Interface {
Name () string//filename
Size () Int64//sang file sizes
Mode () FileMode//file permissions
Modtime () time. Time//Times
Isdir () bool//is the 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
}
[/php]
(6)os.Chmod()The prototype of this function is tofunc Chmod(name string, mode FileMode) errorchange the properties of the file, such as read and write, 0755 on Linux so you can understand it.
[PHP]
Import (
"FMT"
"OS"
)





Func Main () {
FileMode, _: = OS. Stat ("Widuu.go")
Fmt. Println (FileMode. Mode ())//Get permission Linux 0600
ERR: = OS. Chmod ("Widuu.go", 0777)//Change 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





}
[/php]
We'll analyze it today. We will continue to analyze the OS package, this article first in the Micro Network-Network Technology Center http://www.widuu.com/archives/12/908.html then published in my Golang language blog http:// www.lingphp.com forwarding Please specify the source ~ ~ Thank you ~ ~





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

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.