Skip to content

Instantly share code, notes, and snippets.

@developernaren
Created July 26, 2021 05:16
Show Gist options
  • Save developernaren/7152023f76dd4e2e839505fc50622b6b to your computer and use it in GitHub Desktop.
Save developernaren/7152023f76dd4e2e839505fc50622b6b to your computer and use it in GitHub Desktop.
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