How to check if a slice of bytes starts with specific prefix in Go

2 Answers

0 votes
package main

import (
	"fmt"
	"bytes"
)

func main() {
	slice := []byte{'G', 'o', 'l' , 'a', 'n', 'g'} 
	
	fmt.Println(bytes.HasPrefix(slice, []byte("G"))) 
	fmt.Println(bytes.HasPrefix(slice, []byte("a"))) 
}




/*
run:

true
false

*/

 



answered May 26, 2020 by avibootz
0 votes
package main

import (
	"fmt"
	"bytes"
)

func main() {
	fmt.Println(bytes.HasPrefix([]byte("Go lang"), []byte("Go"))) 
	fmt.Println(bytes.HasPrefix([]byte("Go lang"), []byte("abc"))) 
}




/*
run:

true
false

*/

 



answered May 26, 2020 by avibootz

Related questions

2 answers 182 views
4 answers 266 views
2 answers 206 views
2 answers 224 views
2 answers 220 views
1 answer 91 views
...