Library contents

Go os.MkdirTemp

Create private scratch entries with distinct names.

A simple example

os.MkdirTemp

  1. Start with A temporary workspace
  2. Do this Create two independent temporary items
  3. Result Distinct items
Each creation obtains its own temporary location. This lets examples run without overwriting one another.

Syntax

Go syntax
os.MkdirTemp(dir, prefix)
Run these examples in Go

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

go run main.go

Isolate two simultaneous label previews

Two till operators can preview labels with the same product name at the same time. Ask the system to create two independent scratch entries and verify the names differ before writing any shared data.

Resultdistinct
main.go
package main

import (
    "os"
    "fmt"
)

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

func main() {
    dir, err := os.MkdirTemp("", "lesson-")
    check(err)
    defer os.RemoveAll(dir)
    a, err := os.MkdirTemp(dir, "preview-")
    check(err)
    b, err := os.MkdirTemp(dir, "preview-")
    check(err)
    if a != b { fmt.Println("distinct") }
}
Output
distinct

The filesystem creates separate entries for the two jobs.

Advertisements

Locate a rehearsal score

The choir stores each score inside a season folder. Two students use distinct temporary workspaces with the same filename. Does a file saved by the first student appear in the second student’s workspace?

Givenspring / alto.txt
Worked resultSeparate

Locate a workshop handout

An instructor stores the current template in a class folder. Two students use distinct temporary workspaces with the same filename. Does a file saved by the first student appear in the second student’s workspace?

Givenceramics / glaze.pdf
Worked resultSeparate

What to watch for

Keep the handle or path returned by the creation API and clean up after the job.

Documentation for os.MkdirTemp