Created
April 9, 2020 12:12
-
-
Save Bogdaan/be8c8724d3a2711d928c0adda93161fd to your computer and use it in GitHub Desktop.
golang json inline option
This file contains hidden or 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 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, | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was not able to find this in docs anywhere. Can u point me somewhere there?