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.1k Golang : 2 dimensional array example
+2.1k PHP : Proper way to get UTF-8 character or string length
+2.3k Golang : Shortening import identifier
+3.1k Golang : Detect variable or constant type
+6.2k Golang : Google Drive API upload and rename example
+2.5k Golang : Break string into a slice of characters example
+5.8k Golang : Generate Code128 barcode
+3.4k Android Studio : Indicate progression with ProgressBar example
+30.4k Golang : Convert(cast) int64 to string
+2.7k Restart Apache or Nginx web server without password prompt
+7.9k Golang : Find file size(disk usage) with filepath.Walk