Skip to content

Instantly share code, notes, and snippets.

@Bogdaan
Created April 9, 2020 12:12
Show Gist options
  • Save Bogdaan/be8c8724d3a2711d928c0adda93161fd to your computer and use it in GitHub Desktop.
Save Bogdaan/be8c8724d3a2711d928c0adda93161fd to your computer and use it in GitHub Desktop.
golang json inline option
package main
import (
"encoding/json"
"fmt"
)
type Container struct {
Guest1 `json:,inline`
Guest2 `json:"g2"`
}
type Guest1 struct {
Name string
}
type Guest2 struct {
Name string
LastName string
}
func main() {
a := Container{
Guest1: Guest1{
Name: "Tit",
},
Guest2: Guest2{
LastName: "Petric",
},
}
b, _ := json.MarshalIndent(a, "", " ")
fmt.Println(string(b))
var testContainer Container
err := json.Unmarshal([]byte(`{"Name": "Tit"}`), &testContainer)
fmt.Println(
testContainer.Guest1,
testContainer.Guest2,
err,
)
}
@velcrine
Copy link

velcrine commented Sep 5, 2021

I was not able to find this in docs anywhere. Can u point me somewhere there?

@dcrystalj
Copy link

@velcrine it does not exist. Embedding struct itself will automatically trigger inline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment