In Go programming the way to create a directory is to determine if a file exists

  • 2020-05-30 20:22:26
  • OfStack

Determine if the file exists
The os.IsExists () function and os.IsNotExists (), their original form is func IsExist(err error) bool func IsNotExist(err error) bool both pass in 1 err and return bool and notice that err is already defined


  /*
 var (
     ErrInvalid    = errors.New("invalid argument")
     ErrPermission = errors.New("permission denied")
     ErrExist      = errors.New("file already exists")
     ErrNotExist   = errors.New("file does not exist")
 )
*/

So here we see the errors package, so let's talk about the package 1, and the package is just a method called errors.New () and the function is func New(text string) error instance code

import (
 "errors"
 "fmt"
) func main() {
 // Here is 1 A method of func New(text string) error We can define it ourselves
 err := errors.New("widuu blog only golang")
 if err != nil {
  fmt.Println(err) // Here is the output of our own definition of the error message //widuu blog only golang
 }
}

The following is an example of code to explain 1

  import (
 "fmt"
 "os"
) func main() {
 _, err := os.Open("widuu.go")
 if err != nil {
  fmt.Println(os.IsNotExist(err)) //true  The supporting documents already exist
  fmt.Println(err)                //open widuu.go: no such file or directory
 }  // This is how 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 Here's what if file There is no return f The pointer to the file is nil the So we can't use it defer f.Close() The complains
 } // We make a 1 Three file errors already exist to experiment with os.IsExists() The following is Os Defined constants
 /*
  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 is going to output true
 // We'll talk about that in a second link
 err = os.Link("osexists.go", "1.go")
 if err != nil {
  fmt.Println(os.IsExist(err))  // Because I am 1.go This file exists So return true
 }
}


Create a directory
os.Mkdir create a single directory function 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 return the information of an error, let's see, also a review of the previous 1


 import (
 "fmt"
 "os"
) func main() {
 var path string
 if os.IsPathSeparator('\\') {  // Whether the preceding judgment is the system separator
  path = "\\"
 } else {
  path = "/"
 }
 fmt.Println(path)
 dir, _ := os.Getwd()  // Current directory
 err := os.Mkdir(dir+path+"md", os.ModePerm)  // Generate in the current directory md directory
 if err != nil {
  fmt.Println(err)
 }
 fmt.Println(" Create a directory " + dir + path + "md successful ")
}

os.MkdirAll () function is func MkdirAll(path string, perm FileMode) error input is the multilevel directory structure and permissions return is error information

import (
 "fmt"
 "os"
) func main() {
 dir, _ := os.Getwd()
 err := os.MkdirAll(dir+"/a/b/c", os.ModePerm)  // Generate multiple levels of directories
 if err != nil {
  fmt.Println(err)
 }
 fmt.Println(" Create folders " + dir + "/a/b/c successful ")
}


Related articles: