Skip to content

Instantly share code, notes, and snippets.

@dmitry-udod
Created August 30, 2016 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitry-udod/25ffec34abbdbd8648835c4f44832a4f to your computer and use it in GitHub Desktop.
Save dmitry-udod/25ffec34abbdbd8648835c4f44832a4f to your computer and use it in GitHub Desktop.
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