Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Golang convert int or string to int
package main
import (
"encoding/json"
"fmt"
"log"
)
type Object struct {
Num json.Number
}
func main() {
o := &Object{}
err := json.Unmarshal([]byte(`{"Num": "9223372036854775807"}`), o)
if err != nil {
log.Fatal(err)
}
fmt.Println(o.Num.Int64())
err = json.Unmarshal([]byte(`{"Num": 9223372036854775807}`), o)
if err != nil {
log.Println(err)
}
fmt.Println(int(o.Num.Int64()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.