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
+5k Golang : Generate multiplication table from an integer example
+12.1k Swift : Convert (cast) Int to String ?
+4.6k PHP : Convert CSV to JSON with YQL example
+12.8k Golang : Chunk split or divide a string into smaller chunk example
+16.3k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+4.2k Golang : Constant and variable names in native language
+7.9k Golang : Find the length of big.Int variable example
+6.1k Golang : Validate credit card example
+22.8k Golang : How to validate URL the right way
+11.3k Golang : How to display image file or expose CSS, JS files from localhost?
+7.2k Golang : Check if integer is power of four example
+3.9k Golang : A program that contain another program and executes it during run-time