Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - DELETING FILES


To delete a File, we need to import the 'os' module.


We can use the 'Remove()' method of 'os' module to delete a file.


Example :



package main
import "fmt"
import "os"
     
func main() { 
     
    err := os.Remove("myfirstfile.txt")
    if (err != nil) {
        fmt.Println("Couldn't remove file")
    } else {
        fmt.Println("Removed file successfully")
    }	
}


Output :



 Removed file successfully

So, in the first line itself, we have used the, 'Remove()' method of 'os' module to delete the file 'myfirstfile.txt'.


err := os.Remove("myfirstfile.txt")

And if the file couldn't be found, we display the message in the print statement.


if (err != nil) {
    fmt.Println("Couldn't remove file")
}

Since, we have the file, '"myfirstfile.txt' already present. It is removed and we get the below output.


Removed file successfully