SoFunction
Updated on 2025-03-05

In Go programming, it is a method to determine whether a file exists.

Determine whether the file exists
() functions and (), the prototype of their function is func IsExist(err error) bool func IsNotExist(err error) bool both pass an err to return bool. Note here that err has been defined.

Copy the codeThe code is as follows:

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

Here we see the errors package. Let's talk about this package. One method of this package is the () function prototype is func New(text string) error instance code.
Copy the codeThe code is as follows:

import (
 "errors"
 "fmt"
)

func main() {
// Here is a method func New(text string) error we can define ourselves
 err := ("widuu blog only golang")
 if err != nil {
(err) //This is the output of the error message we defined ourselves //widuu blog only golang
 }
}


Let's explain it through the example code below
Copy the codeThe code is as follows:

  import (
 "fmt"
 "os"
)

func main() {
 _, err := ("")
 if err != nil {
((err)) //true  Proof document already exists
  (err)                //open : no such file or directory
 }

// At this time, you can determine whether the file exists in this way

 f, err := ("")
 if err != nil && (err) {
(f, "the file does not exist") //Why is it printed? If the file does not exist, the pointer to return the f file is nil, so we cannot use defer () to report an error.
 }

//We have an error that already exists in the file to experiment with(). The following is the constant defined by Os.
 /*
  var (
      ErrInvalid    = ("invalid argument")
      ErrPermission = ("permission denied")
      ErrExist      = ("file already exists")
      ErrNotExist   = ("file does not exist")
  )
 */
(()) //The output true here
//We will mention link immediately later
 err = ("", "")
 if err != nil {
((err)) //Because my file exists, it returns true
 }
}


Create a directory
Creating 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 and then return an error information. Let's take a look and review the previous knowledge together.

Copy the codeThe code is as follows:

 import (
 "fmt"
 "os"
)

func main() {
 var path string
if ('\\') {  //The previous judgment is the system's separator
  path = "\\"
 } else {
  path = "/"
 }
 (path)
dir, _ := () //Current directory
err := (dir+path+"md", )  // Generate md directory in the current directory
 if err != nil {
  (err)
 }
("Create directory" + dir + path + "md successful")
}


() The original form of the function is func MkdirAll(path string, perm FileMode) error inputs the multi-level directory structure and permissions return the error information.
Copy the codeThe code is as follows:

import (
 "fmt"
 "os"
)

func main() {
 dir, _ := ()
err := (dir+"/a/b/c", )  // Generate multi-level directory
 if err != nil {
  (err)
 }
("Create folder" + dir + "/a/b/c successful")
}