Color theme
0 day streak0 XPP1
GoLearn mode
MISSION 1 OF 3

Plan the ferry stops

0 XP earned
Go track: IntermediateMission: IntermediateSlicesExclusive boundsAppending elements
Before you start this intermediate track

Functions and typed collections. Work with slices, map lookups, and multiple return values.

Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.

x
0 / 6 challenges
LEARN THE CONCEPT

Understand it, then try it.

A ferry planner stores its route in a slice, an ordered sequence that can grow. Slice indexes start at zero. The expression stops[1:3] includes index 1 and stops before index 3. append adds another stop and returns the resulting slice, which must be assigned to keep its new length. This subslice still has spare capacity in its backing array. Appending here can overwrite an element in that shared array; copy the slice first when you need independent storage.

main.go
package mainimport "fmt"func main() {    stops := []string{"Pier", "Cove", "Reef", "Harbor"}    segment := stops[1:3]    segment = append(segment, "Lighthouse")    fmt.Println(segment)}

Each mission is a complete independent Go program. Save it as main.go and run go run main.go with Go 1.18 or later. Only the standard fmt package is used.

Track the values line by line. Predict the result before running this example in your own editor.