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
+18.5k Golang : Example for RSA package functions
+12.4k Golang : Encrypt and decrypt data with x509 crypto
+12.4k Golang : Get month name from date example
+13.5k Golang : error parsing regexp: invalid or unsupported Perl syntax
+7.5k Golang : Example of custom handler for Gorilla's Path usage.
+5.7k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+13.8k Golang : Activate web camera and broadcast out base64 encoded images
+7k Golang : Fibonacci number generator examples
+7.9k Swift : Convert (cast) String to Double
+6k Golang : Extract unicode string from another unicode string example
+7.1k Golang : Validate credit card example
+36.7k Golang : Save image to PNG, JPEG or GIF format.