Golang : Check if a file exist or not
In this tutorial, we will see how to check if a file exist or not before performing further operation.
checkfileexist.go
package main
import (
"fmt"
"os"
)
func main() {
file := "file.txt"
if _, err := os.Stat(file); err == nil {
fmt.Println(file, "exist!")
}
}
if the file.txt
is there, you should see the output
file.txt exist!
See also : Golang : Get file permission
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.8k Golang : How to delete element(data) from map ?
+17k Golang : Get command line arguments
+8.9k Golang : Load ASN1 encoded DSA public key PEM file example
+8.1k Golang : HTTP Routing with Goji example
+6.5k Golang : File system scanning
+33.3k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+5.1k Facebook : How to force facebook to scrape latest URL link data?
+6.1k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+33.9k Golang : Upload and download file to/from AWS S3
+14.4k Golang : rune literal not terminated error
+10.6k Golang : Generate DSA private, public key and PEM files example
+9.3k Golang : Get login name from environment and prompt for password