Range over structs with multiple slices not working

https://play.golang.org/p/sWmlzLWvF4q

In this simple application, I can send HeaderFooter struct data to the template but I can’t parse Chunks struct data into the template.

I would like to range over the ChunkedData and Users in html table.
Only Header and Footer is working

Uncommenting line 44 shows that the data to be chunkd is being distributed amongst all users.

When you say {{range .Chunk}}, the template is essentially attempting to run for _, x := range d.Chunk but you cannot do that because you can only range over arrays, maps or slices. You can instead range over d.Chunk.ChunkData, d.Chunk.ChunkId and/or d.Chunk.ChunkId with {{range .Chunk.ChunkData}}, etc.

1 Like

Thanks a lot
What if am to display the data in an html table apart from li what other html tag can I use.

Or what would be the most efficient way of displaying the data in an html table.

You could change your type definitions to something like this:

type Chunk struct {
    ChunkedData []string
    User string
    ChunkId int
}

type Chunks []Chunk

///Sruct comprising of both Chunks struct and HeaderFooter struct.
///This struct is to be passed to the html template
type Data struct {
	Chunks
	HF    HeaderFooter
}

Which would allow you to write:

Header : {{.HF.Header}}
Footer : {{.HF.Footer}}
{{range .Chunks}}
    {{.ChunkId}} : {{.User}} : {{.ChunkedData}}
{{end}}

Or if you keep your existing types, if I can assume that the indexes into the ChunkedData, Users and ChunkId slices are related (i.e. d.Chunk.ChunkedData[5] is related to d.Chunk.Users[5] and d.Chunk.ChunkId[5], etc.), then you could change your template to this:

Header : {{.HF.Header}}
Footer : {{.HF.Footer}}
{{range $index, $chunk := .Chunk}}
    {{index .Chunk.ChunkId $index}} : {{index .Chunk.User $index}} : {{index .Chunk.ChunkedData $index}}
{{end}}

Thanks for the response.
Would you mind copying the whole code and editing line 89 using the second method

Header : {{.HF.Header}}
Footer : {{.HF.Footer}}
{{range $index, $chunk := .Chunk}}
    {{index .Chunk.ChunkId $index}} : {{index .Chunk.User $index}} : {{index .Chunk.ChunkedData $index}}
{{end}}

This I would like to know how it works.

The plain {{range .Chunk}} pulls the values out of .Chunk and changes the scope of the actions within the {{range}}, so that . now refers to those values. When you add in the variables, {{range $index, $chunk := .Chunk}} it works just like a for index, chunk := range d.Chunk loop. Now that you have an index, you can index into the other slices within d.Chunk. The way you do d.Chunk.Users[index] in Go’s template syntax is {{index .Chunk.Users $index}}

I have already tried it and its not working

Sorry, I misunderstood what you meant. I’ll try it out.

Sorry, I missed some parts in my instructions. Here’s what I meant: https://play.golang.org/p/06881_SHHYd

1 Like

This is what i’ve been wanting all along. Thanks a bunch.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.