Go strings.HasPrefix
Check the beginning of a reference before routing it to the billing queue.
A simple example
strings.HasPrefix
- Start with "abc" and "ab"
- Do this Compare the beginning
- Result Yes
package main
import (
"fmt"
"strings"
)
func main() {
value := "abc"
other := "ab"
if strings.HasPrefix(value, other) {
fmt.Println("yes")
} else {
fmt.Println("no")
}
}
yes
Syntax
strings.HasPrefix(label, "INV-")
Run these examples in Go
Save one example at a time as main.go. Use Go 1.21 or later.
go run main.goRecognize an invoice prefix
A finance inbox recognizes invoices by a specific prefix on their reference codes. Inspect the beginning of the code before placing an incoming record in the invoice queue.
package main
import (
"fmt"
"strings"
)
func main() {
label := "INV-482"
if strings.HasPrefix(label, "INV-") {
fmt.Println("yes")
} else {
fmt.Println("no")
}
}
yes
The first four characters are INV-, so this order enters the invoice queue.
Route a finance request
An intake queue uses a recognizable prefix or status to route incoming work. Check the required position and comparison rules so a matching fragment in an unrelated field cannot send a request to the wrong desk.
package main
import (
"fmt"
"strings"
)
func main() {
value := "FIN-482"
other := "FIN-"
if strings.HasPrefix(value, other) {
fmt.Println("yes")
} else {
fmt.Println("no")
}
}
yes
The prefix identifies the finance queue.
Reject a prefix in the middle
A test label contains a routing token later in its text rather than at the beginning. Confirm that the prefix check rejects it so a keyword inside an unrelated label cannot trigger routing.
package main
import (
"fmt"
"strings"
)
func main() {
value := "OLD-FIN-482"
other := "FIN-"
if strings.HasPrefix(value, other) {
fmt.Println("yes")
} else {
fmt.Println("no")
}
}
no
The expected text occurs later, so this is a different kind of label.
What to watch for
Prefix matching here is case-sensitive. Keep the full identifier when handing it to the next step.