Created
August 30, 2016 13:10
-
-
Save dmitry-udod/25ffec34abbdbd8648835c4f44832a4f to your computer and use it in GitHub Desktop.
Golang convert int or string to int
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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