Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to build a nested, dynamic JSON in Go

package main

import (
    "encoding/json"
    "fmt"
)

type Point struct {
    X, Y int
}

func main() {
    m := map[string]any{}
    var singleCoordinate bool
    if singleCoordinate {
        m["Coordinates"] = []float32{1, 2}
    } else {
        m["Coordinates"] = []Point{{X: 1, Y: 2}, {X: 2, Y: 2}}
    }
    data, err := json.Marshal(m)
    fmt.Println(string(data), err)
}
 
PREVIOUS NEXT
Tagged: #build #dynamic #JSON #Go
ADD COMMENT
Topic
Name
3+7 =