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
+36.3k Golang : How to split or chunking a file to smaller pieces?
+12.6k Golang : flag provided but not defined error
+5.9k Golang : Shuffle array of list
+19.2k Golang : Populate dropdown with html/template example
+6.7k Golang : Skip or discard items of non-interest when iterating example
+11.2k Golang : Calculate Relative Strength Index(RSI) example
+17.4k Golang : Multi threading or run two processes or more example
+5.7k Unix/Linux/MacOSx : Get local IP address
+9.4k Golang : Play .WAV file from command line
+9.7k Golang : interface - when and where to use examples
+10.1k Golang : Identifying Golang HTTP client request
+9k Golang : Inject/embed Javascript before sending out to browser example