Library contents

Go maps.Copy

Apply a set of weekend overrides to the normal opening schedule.

A simple example

maps.Copy

  1. Start with A = 1, B = 2
  2. Do this Merge B = 3 and C = 4
  3. Result A = 1, B = 3, C = 4
One input key replaces an existing value and one introduces a new key.

Syntax

Go syntax
maps.Copy(hours, overrides)
Run these examples in Go

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

go run main.go

Apply weekend opening hours

A library keeps standard opening hours for every day, then receives a short list of weekend changes. The scheduler applies those overrides while preserving weekdays that the weekend list never mentions.

Result10
main.go
package main

import (
    "fmt"
    "maps"
)

func main() {
    hours := map[string]int{"Mon": 9, "Sat": 9}
    maps.Copy(hours, map[string]int{"Sat": 10, "Sun": 11})
    fmt.Println(hours["Sat"])
}
Output
10

The Saturday override changes the displayed hour to ten.

Advertisements

Merge weekend opening hours

The branch receives an updated Saturday timetable. A later set of assignments is merged into the first. Which value does the overlapping key hold?

GivenSat 9 → update 11
Worked result11

Merge a volunteer rota

The organizer imports a replacement Saturday volunteer. A later set of assignments is merged into the first. Which value does the overlapping key hold?

GivenSat Kai → update Mo
Worked resultMo

What to watch for

The destination is the first argument and must support writes.

Documentation for maps.Copy