Go strings.Join
Join two stops with a visible route delimiter.
A simple example
strings.Join
- Start with a and b, separator -
- Do this Join using the separator
- Result a-b
package main
import (
"fmt"
"strings"
)
func main() {
value := "a"
other := "b"
fmt.Println(strings.Join([]string{value, other}, "-"))
}
a-b
Syntax
strings.Join([]string{value, other}, " → ")
Run these examples in Go
Save one example at a time as main.go. Use Go 1.21 or later.
go run main.goPrint a campus shuttle route
A display receives an ordered list of items as separate values. Join them into one readable line with separators between neighboring values and no extra separator after the last one.
package main
import (
"fmt"
"strings"
)
func main() {
value := "Library"
other := "Lab"
fmt.Println(strings.Join([]string{value, other}, " → "))
}
Library → Lab
The arrow preserves the order of the two stops.
Show a parcel handoff
A transit or dispatch system receives journey information as a sequence of stops or handoffs. Preserve their order when interpreting or displaying the route so the next destination is clear.
package main
import (
"fmt"
"strings"
)
func main() {
value := "Depot"
other := "Gate"
fmt.Println(strings.Join([]string{value, other}, " → "))
}
Depot → Gate
The same delimiter separates the names.
Build a parcel shelf identifier
The shelf prefix already includes the hyphen. Combine the two fragments in order without adding another separator. What label goes to the printer?
What to watch for
Joining inserts a delimiter between elements. Include blank elements only if they represent a real stop.