If your string is a file name, and your relevant part is the one without the extension, you can get the extension by filepath.Ext and then chop that off the end.
This also happens with parts of URLs, though - e.g. /section/23-slug . Anything better than regexing it? Because I need to edit that regexp pretty soon.
Not sure if this is what you’re looking for, but you could use something simple like this to retrieve the parts that you wanted from a slug like you posted.
package main
import (
"fmt"
"strings"
)
func main() {
slug := "/section/23-slug"
parts := strings.Split(strings.Trim(slug, "/"), "/")
fmt.Println(parts)
for _, part := range parts {
fmt.Println(part)
}
}
Use the methods from the strings or bytes package to chop the string into the relevant parts.
or
Embrace regular expressions. Once mastered, they can be a valuable tool for many situations where you otherwise would have to resort to hand-crafting some algorithm, maybe ending up with ugly, deeply nested if-then-else cascades that may be more difficult to maintain than a single regular expression.
I admit that regexps are not easy, but there are lots of docs out there and even a couple of online regexp editors/testers that may help coming up to speed.