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
+9.7k Golang : Eroding and dilating image with OpenCV example
+17.3k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+18.8k Unmarshal/Load CSV record into struct in Go
+10.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+18.8k Golang : convert int to string
+10.7k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+21.9k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+13.8k Golang : Qt progress dialog example
+6.2k Java : Human readable password generator
+17.7k Golang : Parse date string and convert to dd-mm-yyyy format
+15.8k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+34k Golang : Call a function after some delay(time.Sleep and Tick)