0

I have a datatype of map[key-string] value-struct, and I'm trying to display a field(Timing) of the struct

I tried all sort of variations for an hour, can't seem to figure it out. Would appreciate any guidance on this, thank you!

Also apologies on the formatting, am new, do bear with me! my code

narwhal06
  • 13
  • 3
  • `$value` or `.` in the outer `range` will be the map values, the struct values. Remove the inner `range`. Please include the code how you execute the template. Also don't post links to images, include all code and template in the question. A link to a runnable example on the [Go playground](https://go.dev/play/) is also welcome. – icza May 06 '22 at 18:49
  • If the values stored in map are not structs having a `Timing` field, you also need to post what they are. Anyway, aim for a [mcve]. – icza May 06 '22 at 18:49

1 Answers1

1

Instead of inner loop, use {{$value.Timing}}.

// You can edit this code!
// Click here and start typing.
package main

import (
    "os"
    "text/template"
)

type A struct {
    Timing string
}

func main() {
    inp := `
    <html>
    
    {{ range $key,$value:= .}}
        Key:{{$key}}, Timing {{$value.Timing}}
    {{end}}
    </html>
`
    valueMap := map[string]A{
        "key": A{
            Timing: "1",
        },
    }
    t, err := template.New("test").Parse(inp)
    if err != nil {
        panic(err)
    }
    err = t.Execute(os.Stdout, valueMap)
    if err != nil {
        panic(err)
    }
}
shubham_asati
  • 623
  • 5
  • 14
  • Hello, thank you for the reply. I changed to {{$value.Timing}} but it is still not showing anything after the first line – narwhal06 May 07 '22 at 07:32
  • Hello again! I missed out the Caps for the field in struct declaration, it works, thank you!! – narwhal06 May 07 '22 at 07:45