Welcome To Golang By Example

Delete a file in Go (Golang)

os.Remove() function can be used to delete a file in Golang. Below is the signature of this function

func Remove(name string) error

Code:

package main

import (
    "log"
    "os"
)

func main() {
    err := os.Remove("sample.txt")
    if err != nil {
        log.Fatal(err)
    }
}

Output:

Deletes sample.txt file from the current working directory