In Go, you can use the 'strings.HasPrefix' function to check if a string starts with a specific prefix.
In this example,
package main
import (
"fmt"
"strings"
)
func startsWithPrefix(str, prefix string) bool {
return strings.HasPrefix(str, prefix)
}
func main() {
str := "Hello World"
prefix := "Hello"
startsWith := startsWithPrefix(str, prefix)
fmt.Println("String starts with prefix '", prefix, "': ", startsWith)
}
String starts with prefix ' Hello ': true
In this tutorial, we learned How to Check if String starts with Specific Prefix in Go language with well detailed examples.