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.2k Golang : Check if integer is power of four example
+4.1k Golang : Convert Chinese UTF8 characters to Pin Yin
+3.6k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+9.1k Golang : Display list of countries and ISO codes
+4.7k Golang : Calculate diameter, circumference, area, sphere surface and volume
+5.1k Golang : Levenshtein distance example
+14.8k Golang : Get all upper case or lower case characters from string example
+5k Golang : Word limiter example
+16.3k Golang : How to read float value from standard input ?
+3.6k PHP : See installed compiled-in-modules
+21.4k Golang : How to read integer value from standard input ?