Created
July 26, 2021 05:16
-
-
Save developernaren/7152023f76dd4e2e839505fc50622b6b to your computer and use it in GitHub Desktop.
This file contains 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" | |
) | |
type City struct { | |
Population float64 `json:"population"` // population in millions | |
Name string `json:"name"` | |
} | |
func main() { | |
ktmStr := `{"name" : "Kathmandu", "population" : "3.5"}` | |
nyStr := `{"name" : "New York", "population" : 8.4 }` | |
var ktm City | |
var ny City | |
json.Unmarshal([]byte(ktmStr), &ktm) | |
json.Unmarshal([]byte(nyStr), &ny) | |
fmt.Println(fmt.Sprintf("population of ktm: %.2f", ktm.Population)) | |
fmt.Println(fmt.Sprintf("population of ny: %.2f", ny.Population)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment