On Wed, May 2, 2018 at 9:32 AM, Tong Sun <suntong...@gmail.com> wrote:
> Thanks. but,
>
> On Wed, May 2, 2018 at 11:27 AM, Burak Serdar wrote:
>>
>> On Wed, May 2, 2018 at 9:16 AM, Tong Sun wrote:
>> >
>> >
>> >
>> >  // now
>> >  env := make(map[string]string)
>> >  for _, e := range os.Environ() {
>> >  sep := strings.Index(e, "=")
>> >  env[e[0:sep]] = e[sep+1:]
>> >  }
>> >  // how to add env into data as ENV so that it can be accessed within
>>
>> args:=map[string]interface{}{"ENV":env}
>> generic(templateStr,args)
>
>
> It looks to me that the above code just pass env (only) to generic, instead
> of adding into data.

Sorry, I didn't look at the code you have in the playground.

You're passing:

sweaters := Inventory{"wool", 17}
generic("{{.Count}} items are made of {{.Material}}\n", sweaters)

You can't add "ENV" to an "Inventory" unless it is already a part of
it. With this code, you're passing in only one Inventory instance.
Template evaluation can only access the public names in that struct,
so you can't add more. However, if you do:

data:=map[string]interface{}{"ENV":env,"Inventory":Inventory{"wool",17}}

and then pass 'data', you can use .ENV.value and  .Inventory.Count in
the template.

Or, you can do the following, and keep the template unchanged:

generic(templateStr,struct {Inventory
  Env map[string]string} {Inventory : Inventory{"wool",17},Env:env})



>
>>
>>
>> > template,
>> >  // e.g., {{.ENV.SHELL}}?
>> > }
>> >
>> >
>> >
>> > Thanks for your help!
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "golang-nuts" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to golang-nuts+unsubscr...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to