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
+6.4k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+2.9k Golang : PGX CopyFrom to insert rows into Postgres database
+10.9k Golang : List running EC2 instances and descriptions
+6.8k Golang : Regular Expression find string example
+7.8k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+53k Golang : Unmarshal JSON from http response
+5.1k Golang : Selection sort example
+6.9k Golang : Add build version and other information in executables
+25.2k Golang : dial tcp: too many colons in address
+12.6k Android Studio : Use image as AlertDialog title with custom layout example
+23.3k Golang : convert rune to integer value
+14.4k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy