Go string slice
Read the second character in a validated three-character ASCII booking code.
A simple example
string slice
- Start with "abc"
- Do this Read the second character
- Result "b"
Syntax
value[1:2]
Run these examples in Go
Save one example at a time as main.go. Use Go 1.21 or later.
go run main.goRead the seat row
A compact printed code assigns meaning to particular positions. Locate the correct character for the requested field and check the indexing convention before sending the value to the display.
package main
import (
"fmt"
)
func main() {
value := "4B7"
fmt.Println(value[1:2])
}
B
The middle character is the row.
Read the lab wing
A lab badge encodes its wing in one character of the printed code. Select that character using the language's indexing rules before directing a visitor to the room.
package main
import (
"fmt"
)
func main() {
value := "2C4"
fmt.Println(value[1:2])
}
C
The second position identifies wing C.
Read a parcel sorting code
The depot scans a parcel marked NC-17. Read the first character to identify the routing area.
What to watch for
Check the length first when accepting arbitrary input. Byte, code-point and UTF-16 indexes can differ outside ASCII.