delete a file in go

We can delete a file in Go using the os package. Here's an example:

main.go
package main

import (
    "fmt"
    "os"
)

func main() {
    fileName := "file.txt"

    err := os.Remove(fileName)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("File deleted successfully.")
}
233 chars
19 lines

In this example, we first define the file name that we want to delete. Then, we use the os.Remove() function to delete the file. If there was an error while deleting the file, we handle the error and print it. Otherwise, we print a success message.

Note that the os.Remove() function permanently deletes the file, so make sure you have a backup or you really want to delete the file before running this code.

gistlibby LogSnag