For the first case use the method strconv
.Itoa()
to convert the numeric value to string and the method len()
to check the size of it. For the second situation, use rune(str)[position]
in which, position
represents the position of string passed as parameter. Remembering you said position 3
, however as the vector starts with 0
, the return on position 3
would be 9
. Behold:
package main
import ("fmt"
"strconv")
func main() {
str := strconv.Itoa(57890)
fmt.Println(len(str))
// index inicia com 0. então 2 representa a posição 3
fmt.Println(string([]rune(str)[0])) // saida 5
fmt.Println(string([]rune(str)[1])) // saida 7
fmt.Println(string([]rune(str)[2])) // saida 8
}
Behold funfando no play golang.
You took an ambiguous example. Could you say who would be index number 0? And index 1?
– Jefferson Quesado
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero