bytes.LastIndexByte() function is used to find sub-byte in byte slice from last in go. It returns the index of sub-byte from last.
LastIndexByte() built-in function of bytes package returns the last instance of sub-byte in byte slice. It returns -1 if sub-byte is not present in byte slice.
bytes.LastIndexByte() function prototype:
func LastIndexByte(s []byte, c byte) int Input parameters: s: byte slices c: byte to be searched in s from last. Return: It returns index of c byte in byte slice s from last.
Explanation on find sub-byte in byte slice from last in go
1) s := []byte("He is all well and well") c := byte('a') Output: 19 2) s = []byte("He is all well and well") c = byte('z') Output: -1 3) s = []byte{10, 20, 30, 20, 30, 20, 40} sep = byte(20) Output: 5
Example:
package main import ( "bytes" "fmt" ) // main function func main() { s := []byte("He is all well and well") c := byte('a') fmt.Println(bytes.LastIndexByte(s, c)) s = []byte("He is all well and well") c = byte('z') fmt.Println(bytes.LastIndexByte(s, c)) s = []byte{10, 20, 30, 20, 30, 20, 40} c = byte(20) fmt.Println(bytes.LastIndexByte(s, c)) }
Output:
15 -1 5
To learn more about golang, Please refer given below link:
References: