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
+4.2k Javascript : How to show different content with noscript?
+13.3k Golang : Verify token from Google Authenticator App
+23.4k Golang : Read a file into an array or slice example
+22.3k Golang : How to read JPG(JPEG), GIF and PNG files ?
+25.1k Golang : Storing cookies in http.CookieJar example
+23.9k Golang : Call function from another package
+39.1k Golang : How to read CSV file
+17.7k Golang : How to make a file read only and set it to writable again?
+14k Golang : Reverse IP address for reverse DNS lookup example
+4.7k Which content-type(MIME type) to use for JSON data
+9.3k Golang : Find the length of big.Int variable example
+20.8k Golang : Convert PNG transparent background image to JPG or JPEG image