Library contents

Go os.Remove

Remove an existing file and check that its name is gone.

A simple example

os.Remove

  1. Start with A practice file exists
  2. Do this Delete that file
  3. Result The path is absent
Testing the same path after successful deletion finds no entry.

Syntax

Go syntax
os.Remove(preview)
Run these examples in Go

Save one example at a time as main.go. Use Go 1.21 or later.

go run main.go

Clear a disposable print preview

A library kiosk creates a preview whenever a borrower asks for a printout. After printing, the kiosk must remove that scratch file and confirm that the job has finished cleaning up.

Resultremoved
main.go
package main

import (
    "os"
    "fmt"
    "path/filepath"
)

func check(err error) {
    if err != nil { panic(err) }
}

func main() {
    dir, err := os.MkdirTemp("", "lesson-")
    check(err)
    defer os.RemoveAll(dir)
    p := func(name string) string { return filepath.Join(dir, name) }
    preview := p("preview")
    check(os.WriteFile(preview, nil, 0600))
    check(os.Remove(preview))
    if _, err := os.Stat(preview); os.IsNotExist(err) { fmt.Println("removed") }
}
Output
removed

The check runs after removal and confirms the preview path is absent.

Advertisements

Prepare a gallery label

A curator stores a short UTF-8 caption beside an exhibit. The desk removes the temporary note after handover. Does its original name still identify an entry?

GivenFile contents “clay”
Worked resultRemoved

Prepare a studio handover

A stage manager saves a two-word instruction for the next shift. The desk removes the temporary note after handover. Does its original name still identify an entry?

GivenFile contents “ring bell”
Worked resultRemoved

What to watch for

Deletion and missing-file behavior vary by API. Limit cleanup to paths created by this job.

Documentation for os.Remove