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
+23.2k Golang : Fix type interface{} has no field or no methods and type assertions example
+13.3k Golang : Check if an integer is negative or positive
+28.2k Golang : Record voice(audio) from microphone to .WAV file
+7k Golang : Scanf function weird error in Windows
+12.4k Golang : http.Get example
+7.1k Golang : Handling Yes No Quit query input
+5.9k Golang : Extract sub-strings
+28.2k Golang : Detect (OS) Operating System
+6.5k Golang : Pat multiplexer routing example
+6.9k Golang : Process json data with Jason package
+4.7k Swift : Convert (cast) Float to Int or Int32 value
+10.8k Golang : Delay or limit HTTP requests example