How to check if a specific element is present in a slice of bytes in Go

2 Answers

0 votes
package main

import (
	"fmt"
	"bytes"
)

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




/*
run:

true
false

*/

 



answered May 26, 2020 by avibootz
0 votes
package main

import (
	"fmt"
	"bytes"
)

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




/*
run:

false
false
true

*/

 



answered May 26, 2020 by avibootz

Related questions

2 answers 183 views
2 answers 180 views
2 answers 225 views
2 answers 167 views
1 answer 147 views
1 answer 230 views
...