I have a string like "en=10 , ab=15". I want to get 10 as an int from this. I cant cast the types it always returns ASCII codes. Should I convert it to map?
Try it like this:
package main
import "strconv"
import "fmt"
func main() {
i, _ := strconv.ParseInt(#YOURSTRING#, 0, 64)
fmt.Println(i)
}
It should then parse it into an int
You may use:
i := strings.Index(st, "=")
then
n, err := fmt.Sscan(st[i+1:], &d)
try this:
package main
import (
"fmt"
"log"
"strings"
)
func main() {
st := "en=10 , ab=15"
i := strings.Index(st, "=")
if i != -1 {
var d int
n, err := fmt.Sscan(st[i+1:], &d)
if err != nil || n != 1 {
log.Fatal(err)
}
fmt.Println(d)
}
}
output:
10
You should split the string and then convert it the individual components to int.
var splits = strings.Replace("en=10 , ab=15", ",", "=")
splits = strings.split(splits, "=")
for var i := 0; i < splits.Len(); i++ {
splits[i].toInt()
}