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