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
+16.7k Golang : Get input from keyboard
+7.8k Findstr command the Grep equivalent for Windows
+10.9k Golang : How to determine a prime number?
+10.4k Golang : How to delete element(data) from map ?
+9.9k Golang : Bcrypting password
+17.4k Golang : Defer function inside init()
+7.5k Golang : Test if an input is an Armstrong number example
+11.5k Golang : convert(cast) float to string
+5.7k Unix/Linux : How to open tar.gz file ?
+8k Golang : HttpRouter multiplexer routing example
+5.1k Responsive Google Adsense