Golang bytes.HasPrefix() function example
package bytes
HasPrefix tests whether the byte slice begins with prefix.
Golang bytes.HasPrefix() function usage example
package main
import (
"bytes"
"fmt"
)
func main() {
str := []byte("abcdef")
fmt.Println(bytes.HasPrefix(str, []byte("abc"))) // true because string starts with abc
fmt.Println(bytes.HasPrefix(str, []byte("bcd"))) // false
fmt.Println(bytes.HasPrefix(str, []byte("def"))) // false
}
Output :
true
false
false
Note :
Prefix = a word, letter, or number placed before another.
See also : Golang bytes.HasSuffix() function example
Advertisement
Something interesting
Tutorials
+35.3k Golang : Strip slashes from string example
+7.3k Golang : File system scanning
+5.7k Linux/Unix/PHP : Restart PHP-FPM
+38.1k Golang : Read a text file and replace certain words
+9.6k Golang : Sort and reverse sort a slice of floats
+7.6k Javascript : Push notifications to browser with Push.js
+4.7k JavaScript: Add marker function on Google Map
+6.7k Golang : Derive cryptographic key from passwords with Argon2
+7.9k Golang : Grayscale Image
+20k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+6.1k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+14.8k Golang : Normalize unicode strings for comparison purpose