Standard library / os

os.Remove()

Removes a named file or empty directory. It does not recursively remove a nonempty directory. Example uses a disposable temporary workspace. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func Remove(name string) error

Parameters & Attributes

NameTypeDescription
namestringName or pathname of the target.

Return Value

error

Removes a named file or empty directory.

Working Example

Example Snippetgo
package main
import (  "errors"  "fmt"  "os")
func check(e error) {  if e != nil {    panic(e)  }}func seed() { check(os.WriteFile("a.txt", []byte("abc"), 0600)) }func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() {  defer workspace()()  seed()  check(os.Remove("a.txt"))  _, e := os.Stat("a.txt")  fmt.Println(errors.Is(e, os.ErrNotExist))}
Go Reference