In Go, you can use the strings.HasPrefix
function from the strings
package to check if a string starts with a specific prefix.
Here's an example of how you can use it:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello, World!"
prefix := "Hello"
if strings.HasPrefix(str, prefix) {
fmt.Printf("'%s' starts with '%s'\n", str, prefix)
} else {
fmt.Printf("'%s' does not start with '%s'\n", str, prefix)
}
}
In the example above, the strings.HasPrefix
function is used to check if the string str
starts with the prefix Hello
. If it does, it will print that the string starts with the prefix. Otherwise, it will print that the string does not start with the prefix.