Golang Array Of Struct To JSON

package main

import (
"log"
"encoding/json"
)

type Fruit struct {
Name string `json:"name"`
Quantity int `json:"quantity"`
}

func main() {
a := Fruit{Name:"Apple",Quantity:1}
o := Fruit{Name:"Orange",Quantity:2}

var fs []Fruit
fs = append(fs, a)
fs = append(fs, o)
log.Println(fs)

j, _ := json.Marshal(fs)
log.Println(string(j))

j, _ = json.MarshalIndent(fs, "", " ")
log.Println(string(j))
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevin FOO

Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.