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
+11.9k Golang : Find and draw contours with OpenCV example
+14k Golang : Check if a file exist or not
+5.9k PHP : Get client IP address
+9.2k Golang : Apply Histogram Equalization to color images
+10.9k Golang : Create S3 bucket with official aws-sdk-go package
+14.4k Golang : Rename directory
+11.5k Get form post value in Go
+12.7k Golang : http.Get example
+20.1k Golang : Check if os.Stdin input data is piped or from terminal
+23.4k Golang : Check if element exist in map
+9.7k Golang : ffmpeg with os/exec.Command() returns non-zero status