How to find the first index value in slice of bytes in Golang?
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice.
In the Go slice of bytes, you can find the first index value of the specified instance in the given slice using Index() function. This function returns the index of the first instance of the given value in the original slice of bytes. It returns -1 if the given value is not available in the original slice. It is defined under the bytes package so, you have to import bytes package in your program for accessing Index function.
func Index(ori_slice, sep_ byte) int
Here, ori_slice is the original slice and sep_ is a slice, whose we want to find the first index value. Let us discuss this concept with the help of the given examples:
Final Value: Slice 1: 16 Slice 2: -1 Slice 3: 1
Original Slice: Slice 1: !!GeeksforGeeks## Slice 2: Apple Slice 3: %geeks% First Index: Slice 1: 3 Slice 2: 2 Slice 3: -1