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
+2.4k Linux : Disable and enable IPv4 forwarding
+3.5k Golang : Apply Histogram Equalization to color images
+4.7k Golang : Resolve domain name to IP4 and IP6 addresses.
+1.9k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+13.2k Golang : Force your program to run with root permissions
877 Linux : How to set root password in Linux Mint
+1.9k Golang : Gonum standard normal random numbers example
+18.2k Golang : Interpolating or substituting variables in string examples
+2.4k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+4.5k Golang : How to get ECDSA curve and parameters data?
+5.6k Golang : Get dimension(width and height) of image file
+3.4k Golang : Calculate Relative Strength Index(RSI) example