Golang os.IsExist() and IsNotExist() functions example
package os
Golang os.IsExist() and IsNotExist() functions usage example. Useful in determining if a file or directory already exist or already removed/deleted
package main
import (
"fmt"
"os"
)
func main() {
err := os.Mkdir("testdir", 0700)
// testdir directory already exist?
if os.IsExist(err) {
err = nil // then nullify the error
}
if err != nil {
panic(err)
}
fmt.Println("testdir directory created")
// -------------------------
err = os.Remove("testdir")
if err == nil || os.IsNotExist(err) {
fmt.Println("testdir directory already removed/deleted")
return
}
if err != nil {
panic(err)
}
fmt.Println("testdir directory removed/deleted")
}
See also :
https://www.socketloop.com/tutorials/golang-check-if-a-directory-exist-in-go
https://www.socketloop.com/tutorials/golang-check-if-a-file-exist-or-not
References :
Advertisement
Something interesting
Tutorials
+12k Golang : Decompress zlib file example
+7.8k Golang : Load DSA public key from file example
+9.2k Golang : Create and shuffle deck of cards example
+10.1k Golang : How to tokenize source code with text/scanner package?
+13.7k Golang : Tutorial on loading GOB and PEM files
+11.6k Get form post value in Go
+15.6k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+17.6k Golang : Parse date string and convert to dd-mm-yyyy format
+21.8k SSL : How to check if current certificate is sha1 or sha2
+9.8k Golang : Resumable upload to Google Drive(RESTful) example
+13k Swift : Convert (cast) Int to String ?