How to find the last index value in slice of bytes in Golang?
Last Updated :
29 Oct, 2024
In Go, a slice is a flexible data structure that stores a variable-length sequence of elements of the same type. The LastIndex()
function from the bytes
package returns the last index of a specified value in a byte slice, or -1 if the value is not found.
Example
package main
import (
"bytes"
"fmt"
)
func main() {
// Define the slice of bytes
slice := []byte("GeeksforGeeks, Learning Go")
// Define the byte to search for
searchByte := []byte("e")
// Find the last index of the specified byte
lastIndex := bytes.LastIndex(slice, searchByte)
// Display the result
fmt.Printf("\n", searchByte, lastIndex)
}
Syntax
func LastIndex(ori_slice, sep_ []byte) int # Using the bytes.LastIndex
Function
Using the bytes.LastIndex
Function
The simplest way to find the last index of a specified byte slice is to use the LastIndex
function from the bytes
package.
Go
package main
import (
"bytes"
"fmt"
)
func main() {
originalSlice := []byte("Welcome to GeeksforGeeks")
searchSlice := []byte("ks")
lastIndex := bytes.LastIndex(originalSlice, searchSlice)
fmt.Printf("%s: %d\n", searchSlice, lastIndex)
}
Other method's:
Apart from the simple method mentioned above,there are few more methods which we can use to find the last index value in slice of bytes in Golang.
Manual Iteration through the Slice
You can manually iterate through the slice in reverse order to find the last index of the specified byte value.
Go
package main
import "fmt"
func lastIndexManual(slice []byte, value byte) int {
for i := len(slice) - 1; i >= 0; i-- {
if slice[i] == value {
return i
}
}
return -1
}
func main() {
slice := []byte("GeeksforGeeks")
value := byte('e')
lastIndex := lastIndexManual(slice, value)
fmt.Printf("%c: %d\n", value, lastIndex)
}
Using a Loop with bytes.Equal
You can create a custom function that checks for equality between slices to find the last index.
Example
Go
package main
import (
"bytes"
"fmt"
)
func lastIndexEqual(slice []byte, target []byte) int {
for i := len(slice) - len(target); i >= 0; i-- {
if bytes.Equal(slice[i:i+len(target)], target) {
return i
}
}
return -1
}
func main() {
slice := []byte("Geeks for Geeks")
target := []byte("for")
lastIndex := lastIndexEqual(slice, target)
fmt.Printf("%s: %d\n", target, lastIndex)
}
Using strings.LastIndex
for String Conversion
Convert the byte slice to a string and use the strings.LastIndex
function to find the last index.
Go
package main
import (
"fmt"
"strings"
)
func main() {
originalSlice := []byte("Geeks for Geeks")
searchString := "for"
lastIndex := strings.LastIndex(string(originalSlice), searchString)
fmt.Printf("'%s': %d\n", searchString, lastIndex)
}
Similar Reads
Golang | How to find the index of rune in the string? In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In the Go strings, you can also find the first index of the specified rune in
3 min read
How to split a slice of bytes in Golang? In Golang, you can split a slice of bytes into multiple parts using the bytes.Split function. This is useful when dealing with data like encoded strings, file contents, or byte streams that must be divided by a specific delimiter.Examplepackage mainimport ( "bytes" "fmt")func main() { // Initial byt
3 min read
How to sort a slice of ints in Golang? In Go, slices provide a flexible way to manage sequences of elements. To sort a slice of ints, the sort package offers a few straightforward functions. In this article we will learn How to Sort a Slice of Ints in Golang.ExampleGopackage main import ( "fmt" "sort" ) func main() { intSlice := []int{42
2 min read
How to trim a 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 ar
3 min read
How to compare two slices of bytes in Golang? In Go, a slice is a powerful, flexible data structure that allows elements of the same type to be stored in a variable-length sequence. When working with byte slices ([]byte), Go provides easy-to-use functions in the bytes package to compare them. In this article we will learn "How to compare two sl
2 min read