The HasPrefix() Function is used to check if a String begins with a given set of Characters.
package main import "fmt" import "strings" func main() { x := "Hello Beautiful World" y := strings.HasPrefix(x, "Hel") if (y == true) { fmt.Println("The String begins with the sub string Hel") } else { fmt.Println("The String does not begins with the sub string Hel") } }
In the above code, we have declared a String 'Hello Beautiful World' and assigned it to a variable 'x'.
And we would be checking if the String 'Hello Beautiful World' starts with 'Hel'.
So, we have used the 'HasPrefix()' Function to check.
And what 'HasPrefix()' Function does is, goes to the String 'Hello Beautiful World' and searches if the String begins with 'Hel'.
And finds that the String 'Hello Beautiful World' begins with 'Hel'.
So the control gets into the 'if' block and the below output is printed.