Golang (go language) standard library analysis OS (3)

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

Golang Standard Library

Today we continue to analyze the OS package, this package is actually a lot of estimated to be a few days before we can analyze this package – and thank you for your continued attention!!! I look at the online search Mac Golang More, I will also use Mac OS development! My environment

The first one we're talking about is the OS. Isexists () function and OS. Isnotexists (), the prototype of their function is the Func isexist (err error) bool Func isnotexist (ERR Error) bool is passed in an ERR return bool Here Note that the ERR has been defined well
[PHP]
/*
VAR (
Errinvalid = errors. New ("Invalid Argument")
errpermission = errors. New ("Permission Denied")
Errexist = errors. New ("File already exists")
Errnotexist = errors. New ("File does not exist")
)
*/
[/php]
Here we see errors this bag, then we talk about this bag, this package is one way is errors. The prototype of the new () function is the func new (text string) Error instance code
[PHP]
Import (
"Errors"
"FMT"
)

Func Main () {
Here's a method. Func New (text string) error we can define ourselves
ERR: = errors. New ("Widuu blog Only Golang")
If err! = Nil {
Fmt. PRINTLN (ERR)//This is the output of our own defined error message//widuu blog only Golang
}
}
[/php]
Here's an example code to explain
[PHP]
Import (
"FMT"
"OS"
)

Func Main () {
_, Err: = OS. Open ("Widuu.go")
If err! = Nil {
Fmt. Println (OS. Isnotexist (ERR))//true proof file already exists
Fmt. PRINTLN (ERR)//open widuu.go:no such file or directory
}

This time you can tell if a file exists

F, err: = OS. Open ("Widuu.go")
If err! = Nil && os. Isnotexist (Err) {
FMT. Println (F, "file does not exist")//Why print nil is such that if file does not exist the pointer to return F files is nil, so we cannot use defer f.close () will error
}
[/php]
// We make a file that already exists error to experiment with the OS. Isexists () follows the OS-defined constants
[PHP]
/*
var (
Errinvalid = errors. New ("Invalid Argument")
Errpermission = errors. New ("Permission Denied")
Errexist = errors. New ("File already exists")
Errnotexist = errors. New ("File does not exist")
)
*/
FMT. Println (OS. Isexist (OS. errexist)///This will output true
//We'll mention link
Err = os immediately behind us. Link ("Osexists.go", "1.go")
If err! = Nil {
FMT. Println (OS. Isexist (ERR))//Because I 1.go this file exists so that it returns True
}
}
[/php]
The second one is the OS. The prototype of the Ispathseparator () function is the Func ispathseparator (c uint8) bool Note that the bytes passed in is "cannot be used" and this is very simple to determine whether the system delimiter
[PHP]
Import (
" FMT "
" OS "
)

Func Main () {
Note that the argument must be "single quotation mark cannot be" "because it is a character type
Fmt. Println (OS. Ispathseparator ('/'))//I am a Mac OS so/is the directory separator
Fmt. Println (OS. Ispathseparator (' \ \ '))//This is not
Fmt. Println (OS. Ispathseparator ('. ')) That's not it.
}
[/php]
The third explanation is the OS. Ispermission () is the same as the above is judged, this is to judge that there is a permission to the wood, the function of the prototype is the Func ispermission (err error) bool Return is still the bool type, the instance code
[PHP]
Import (
"FMT"
"OS"
)

Func Main () {

We put chmod 1.go.
_, Err: = OS. Open ("1.go")
If err! = Nil {
Fmt. Println (OS. Ispermission (err))//true
Fmt. PRINTLN (Err)//open 1.go:permission denied
}
}
[/php]
The fourth one is the OS. Lchown () The prototype of this function is the Func lchown (name string, UID, gid int) error, seemingly simple function contains a lot of knowledge, such as UID and GID is a file like my current environment of-rwxr-xr-x 1. The user ID and group ID that go belongs to, and the instance code explains what's involved.
[PHP]
Import (
"FMT"
"OS"
"Reflect"
"Syscall"
)

Func Main () {
F, err: = OS. Stat ("Widuu1.go")//The front
If err! = Nil {
Fmt. PRINTLN (ERR)
}
A: = reflect. ValueOf (F.sys ())//This people may not understand what it means so I use reflection to show you this value type
Fmt. Println (a)//<*syscall. stat_t value>
/*
Type stat_t struct {
Dev int32
Mode UInt16
Nlink UInt16
Ino UInt64
Uid UInt32
Gid UInt32
Rdev int32
pad_cgo_0 [4]byte
Atimespec Timespec
Mtimespec Timespec
Ctimespec Timespec
Birthtimespec Timespec
Size Int64
Blocks Int64
Blksize int32
Flags UInt32
Gen UInt32
Lspare int32
Qspare [2]int64
}
*/

Then we get the UID Gid with the value map.
Fmt. Println (F.sys (). ( *syscall. stat_t). Uid, F.sys (). (*syscall. stat_t). GID)//My Mac 501 20

Err = OS. Lchown ("Widuu1.go", 100, 100)
If err! = Nil {
Fmt. Println (OS. Ispermission (ERR))//Note here it is possible that you did not modify the permissions if True is definitely a modification unsuccessful
}
FI, err: = OS. Stat ("Widuu1.go")
If err! = Nil {
Fmt. PRINTLN (ERR)
}
Fmt. Println (FI. Sys (). (*syscall. stat_t). Uid, FI. Sys (). (*syscall. stat_t). Gid)//100 100
}
[/php]
The fifth one is the OS. Link () function prototype is the Func link (oldname, newname string) error creating a hard connection – understand Linux for sure understanding
Give an example
[PHP]
Import (
"FMT"
"OS"
)

Func Main () {
ERR: = OS. Link ("Link.go", "Widuu2.go")
If err! = Nil {
Fmt. Println (OS. Isexists (ERR))//Here may be a file that already exists error
}
Fmt. Println ("Widuu2.go create")

}
[/php]

If you prefer the Golang standard library analysis of the Micro Network, please continue to follow us

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

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.