[Golang] Check if File, Directory, or Symlink Exist


Check if a file, directory, or symbolic link exists in Golang:

import "os"

func IsExist(path string) bool {
      if _, err := os.Stat(path); err == nil {
              // exist
              return true
      }
      // not exist
      return false
}

If you want to check if NOT exist:

import "os"

func IsNotExist(path string) bool {
      if _, err := os.Stat(path); os.IsNotExist(err) {
              // not exist
              return true
      }
      // exist
      return false
}

References:

[1]
[2]go - How to check whether a file or directory denoted by a path exists in Golang? - Stack Overflow