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
+8k Golang : Delete files by extension
+18.6k Golang : Strings to lowercase and uppercase example
+5.9k Golang : Generate random integer or float number
+5.2k Golang : Convert int(year) to time.Time type
+2k Golang : How to setup a disk space used monitoring service with Telegram bot
+4.6k Golang : Append and add item in slice
+4.9k Golang : Validate IPv6 example
+1.5k Golang : Convert an executable file into []byte example
+4.8k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+2.4k Python : Convert(cast) bytes to string example
+2.5k Golang : Populate slice with sequential integers example