Library contents

Go Two-result map lookup

Check whether a parcel is registered even if a driver has yet to be assigned.

A simple example

Two-result map lookup

  1. Start with A = 2
  2. Do this Look for key A
  3. Result Present
The letter identifies the entry. Two is the value stored at that key.

Syntax

Go syntax
driver, exists := drivers[key]
Run these examples in Go

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

go run main.go

Find a parcel whose driver is still unassigned

A dispatch desk registers parcels before assigning drivers. Parcel P7 already has a record with an empty assignment, and the operator must distinguish it from a parcel that has never been registered.

ResultRegistered
main.go
package main

import (
    "fmt"
)

func main() {
    drivers := map[string]*string{"P7": nil}
    if _, exists := drivers["P7"]; exists {
        fmt.Println("Registered")
    }
}
Output
Registered

P7 appears in the register and the driver field is still unassigned.

Advertisements

Find an unassigned booking

A clinic already has booking B12, although a clinician has yet to be assigned. The desk checks whether the identifier has a record before creating one. Is the key already present?

GivenB12 → unassigned
Worked resultPresent

Look for a second ticket

A counter has recorded ticket T2 and receives a query for T3. The desk checks whether the identifier has a record before creating one. Is the key already present?

GivenT2 → ready · query T3
Worked resultAbsent

What to watch for

The zero value from a single-result lookup cannot distinguish a missing entry from a present nil driver.

Documentation for Two-result map lookup