Go programming to determine whether a file exists is a way to create a directory _golang

Source: Internet
Author: User
Tags create directory function prototype mkdir

To determine whether a file exists
OS. Isexists () function and OS. Isnotexists (), the prototype of their function is func isexist (err error) bool Func isnotexist (ERR Error) bool is passed in a err to return to bool here Note that ERR has been defined.

Copy Code code as follows:

/*
VAR (
Errinvalid = errors. New ("Invalid Argument")
errpermission = errors. New ("Permission Denied")
Errexist = errors. New ("File already exists")
Errnotexist = errors. New ("File does not exist")
)
*/

Here we see errors this bag, so let's talk about this bag, and this package is just one way to errors. The new () function prototype is the Func new (text string) Error instance code
Copy Code code as follows:

Import (
"Errors"
"FMT"
)

Func Main () {
Here's one way to Func New (text string) error We can define it ourselves.
ERR: = errors. New ("Widuu blog Only Golang")
If Err!= nil {
Fmt. PRINTLN (ERR)//This is the output of our own definition of the error message//widuu blog only Golang
}
}


The following is explained by the example code
Copy Code code as follows:

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
}

At this point, you can tell if the 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 so if file does not exist the pointer to the F file is nil, so we cannot use defer f.close () to make an error.
}

We make a file that already exists in error to experiment with the OS. Isexists () is the constant of the OS definition as follows
/*
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 talk about link right behind us.
Err = OS. Link ("Osexists.go", "1.go")
If Err!= nil {
Fmt. Println (OS. Isexist (ERR))//Because I 1.go this file exists so say return true
}
}


Create a table of contents
OS. Mkdir Create a single directory function prototype func Mkdir (name string, perm FileMode) Error Enter the name of a directory and the permissions of the directory, we can use the default OS. Modeperm and then return is an error information, we look at, also a piece of review the front of a little knowledge

Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
var path string
If OS. Ispathseparator (' \ \ ') {//The front of the judge is the system separator
Path = "\"
} else {
Path = "/"
}
Fmt. PRINTLN (PATH)
Dir, _: = OS. GETWD ()//current directory
ERR: = OS. Mkdir (dir+path+ "MD", OS. Modeperm)//Generate MD directory in current directory
If Err!= nil {
Fmt. PRINTLN (ERR)
}
Fmt. PRINTLN ("Create directory" + dir + path + "MD Success")
}


Os. The Mkdirall () function is the Func mkdirall (path string, perm FileMode) error entered the multilevel directory structure and the information that the permission returns is error
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
Dir, _: = OS. GETWD ()
ERR: = OS. Mkdirall (dir+ "/a/b/c", OS. Modeperm)//Generate multilevel Catalogs
If Err!= nil {
Fmt. PRINTLN (ERR)
}
Fmt. Println ("Create folder" + Dir + "/a/b/c successful")
}

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.