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
+33.3k Golang : How to check if slice or array is empty?
+24.9k Golang : convert rune to integer value
+5.3k Golang : Stop goroutine without channel
+5.4k Golang : Shortening import identifier
+7.3k Gogland : Single File versus Go Application Run Configurations
+20.1k Nginx + FastCGI + Go Setup.
+15.3k Golang : Get checkbox or extract multipart form data value example
+6.7k Golang : Calculate BMI and risk category
+9.8k Golang : Get login name from environment and prompt for password
+25.4k Golang : How to write CSV data to file
+14.2k Golang : Reset buffer example
+19.1k Golang : Get current URL example