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
+19.5k Golang : Clean up null characters from input data
+3.7k MariaDB/MySQL : How to get version information
+13.3k Golang : How to shuffle elements in array or slice?
+12.6k Golang : Compress and decompress file with compress/flate example
+18.3k Golang : Archive directory with tar and gzip
+10.4k Golang : Surveillance with web camera and OpenCV
+12.5k Golang : Activate web camera and broadcast out base64 encoded images
+13.4k Golang : Get URI segments by number and assign as variable example
+10.5k Golang : Get remaining text such as id or filename after last segment in URL path
+4.7k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday