5

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

 2 years ago
source link: http://siongui.github.io/2017/03/27/go-check-if-file-folder-symlink-exist/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

March 27, 2017

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:

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

[3]Package write provides a way to atomically create or replace a file or symbolic link. : golang


Author: Siong-Ui Te Category: Go

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK