How to find the last 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 last index value of the specified instance the given slice using LastIndex() function. This function returns the index of the last 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 LastIndex function.
func LastIndex(ori_slice, sep_ byte) int
Here, ori_slice is the original slice and sep_ is a slice, whose we want to find the last index value. Let us discuss this concept with the help of the given examples:
Final Value: Slice 1: 26 Slice 2: 34 Slice 3: -1
Original Slice: Slice 1: !!GeeksforGeeks## Slice 2: Apple Slice 3: %geeks% Last Index: Slice 1: 12 Slice 2: 0 Slice 3: -1