0

I want to create result object json with dynamic structure of data, ex in func1 the result is like this

{
  'result': 'this is result',
  'content': {
    'func1'  : 'value',
    'some_desc_func1': 'value'
  }
}

and func2 maybe the result is just (focus on content) like this

{
  'result': 'this is result',
  'content': {
    'func2'  : 'value'
  }
}

As this reference https://stackoverflow.com/a/35657622/4476788, i want to show result json with just one key of result.

Like this

{
  'result': 'this is result',
  'content': {
    'key'  : 'value'
  }
}

And not like this

[
  {
    'result_1' : 'answer 1'
  },
  {
    'result_2' : 'answer 2'
  }
]

i try to update the play ground of the answer but it showing error

type Brand struct {
   Name string
}

var database map[string]interface{}

func init() {
  database = make(map[string]interface{})

  brands := make([]Brand, 1)
  brands = Brand{"Gucci"}

  database["brands"] = brands
} 

You can try run in here https://play.golang.org/p/mKCwKEVI7E

it showing error

tmp/sandbox651609402/main.go:22: cannot use Brand literal (type Brand) as type []Brand in assignment

3
  • brands[0] = Brand{"Gucci"} Commented Dec 9, 2016 at 6:26
  • it will showing result with [] (square brackets), how to remove it? @nu11p01n73R Commented Dec 9, 2016 at 6:28
  • 1
    @AlanEl-ninoMalmsteen, you see square brackets because it is a slice. What do you want to see instead? Commented Dec 9, 2016 at 6:37

1 Answer 1

1

Line 22 should be: brands = []Brand{Brand{"Gucci"}}

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.