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
+12.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+6.9k Golang : Configure Apache and NGINX to access your Go service example
+8.7k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+13.6k Golang : Search folders for file recursively with wildcard support
+4.8k Cash Flow : 50 days to pay your credit card debt
+18.3k Golang : Close channel after ticker stopped example
+9.4k Golang : ISO8601 Duration Parser example
+8.2k Golang : Create unique title slugs example
+12k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+4.2k Unix/Linux : secure copying between servers with SCP command examples
+25.7k Golang : Find files by name - cross platform example
+9.5k Golang : Create matrix with Gonum Matrix package example