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
+12.5k Android Studio : Highlight ImageButton when pressed on example
+22.7k Golang : simulate tail -f or read last line from log file example
+6.3k Golang : Handling image beyond OpenCV video capture boundary
+9.5k Golang : Quadratic example
+34.9k Golang : Smarter Error Handling with strings.Contains()
+8.2k Golang : Count leading or ending zeros(any item of interest) example
+11.6k Golang : Secure file deletion with wipe example
+8.7k Golang : Get final balance from bit coin address example
+15.7k Golang : Get current time from the Internet time server(ntp) example
+9.7k Golang : Resumable upload to Google Drive(RESTful) example
+4.6k JavaScript: Add marker function on Google Map