Library contents

Go Two-index slice expression

Read the middle batch of an order queue using a range.

A simple example

Two-index slice expression

  1. Start with A, B, C
  2. Do this Select the first two items
  3. Result A, B
The selected range ends before C.

Syntax

Go syntax
orders[start:end]
Run these examples in Go

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

go run main.go

Select two orders for the packing bench

A packing bench takes a small batch from the middle of the day's queue. The supervisor wants orders B and C on this bench while the other orders stay visible in the full queue.

From the programorders := []string{"A", "B", "C", "D"}
ResultB, C
main.go
package main

import (
    "fmt"
    "strings"
)

func main() {
    orders := []string{"A", "B", "C", "D"}
    fmt.Println(strings.Join(orders[1:3], ", "))
}
Output
B, C

The requested range contains only orders B and C.

Advertisements

Prepare the next workshop roster

A teacher reads a workshop roster in arrival order. The coordinator prints the first two entries on a separate slip. Which entries appear, in order?

GivenMira · Ivo · Bea
Worked resultMira · Ivo

Work from the delivery round

A courier has three labelled stops in route order. The coordinator prints the first two entries on a separate slip. Which entries appear, in order?

GivenPark · Mill · Pier
Worked resultPark · Mill

What to watch for

The result shares backing storage with the input. Clone it when later edits must be independent.

Documentation for Two-index slice expression