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
+5.9k Golang : Convert(cast) io.Reader type to string
+10k Golang : Arithmetic operation with numerical slices or arrays example
+19.5k Golang : Convert(cast) string to rune and back to string example
+3.1k Adding Skype actions such as call and chat into web page examples
+9.1k Golang : Sort and reverse sort a slice of runes
+6.7k Golang : Intercept and compare HTTP response code example
+31.2k Golang : Validate IP address
+7.4k Golang : How to control fmt or log print format?
+9.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+12k Golang : package is not in GOROOT during compilation
+7.4k Golang : does not implement flag.Value (missing Set method)