ErickSkrauch ErickSkrauch - 1 year ago 281
JSON Question

How to serialize to json embed structure in golang

I'm just starting to learn Go, but I can not make out current task. I need to serialize the structure consisting of nested structures.

package main

import (

type Metadata struct {
model string

type Texture struct {
url string
hash string
metadata *Metadata

type Response struct {
SKIN *Texture

func main() {
response := Response{}
textures := &Texture{
url: "",
hash: "123123123123123123",
metadata := &Metadata{
model: "slim",

textures.metadata = metadata
response.SKIN = textures

result, _ := json.Marshal(response)

Always output only {"SKIN":{}}. Expected value is:

"SKIN": {
"url": "",
"hash": "123123123123123123",
"metadata": {
"model": "slim"

I created this example in a sandbox

Answer Source

You need to export your fields (make the names capitalized):

type Metadata struct {
    Model string

type Texture struct {
    Url      string
    Hash     string
    Metadata *Metadata

Updated playground example:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download