I want to know how I can get the last character of any string with GO. So far the only thing I could do is get each character from a string.
for i := 0; i < len(palabra); i++ {
fmt.Println(string(len(palabra) [i]))
}
I want to know how I can get the last character of any string with GO. So far the only thing I could do is get each character from a string.
for i := 0; i < len(palabra); i++ {
fmt.Println(string(len(palabra) [i]))
}
As is well known in Go You can extract Sub-Slices Following the nomenclature Slice[InitialValue:FinalValue] and in turn we know that the Strings are a Slice of Characters, why not do the same?
In this case our slice is called val , We get the last element with len(val)-1 (Note: The len method returns the size of a Slice in this case -1 because we want to get the last element)
Here I leave you an Online solution for you to review: Example And an Official Documentation about slices in go: Slices: Uses
I already solved it, I assigned a variable that contains each letter when iterating and therefore its value, at the end of the iteration, would be the last letter. I leave the code.