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
+8.1k Golang : Quadratic example
+7.5k Golang : Simple histogram example
+11.6k Golang : List objects in AWS S3 bucket
+27.7k Golang : Login(Authenticate) with Facebook example
+11.7k Golang : Calculate elapsed years or months since a date
+11.1k Golang : HTTP response JSON encoded data
+18.3k Golang : How to force compile or remove object files first before rebuild?
+8.2k Golang : Turn string or text file into slice example
+6.6k Golang Hello World Example
+10.3k Golang : Save webcamera frames to video file
+3.9k Golang : Get FX sentiment from website example