N
N
Nube2018-06-01 15:10:46
go
Nube, 2018-06-01 15:10:46

How to populate a nested structure with data?

I'm trying to make a post that displays several images. The problem is that if with one picture of the rules, then with several bummers.
There is a structure

type Post struct {
  ID   		        int
  Rating		int
  Title		        string
  Description      string
  Author     	string
  Images		*[]ImagePost
}

type ImagePost struct {
  ImageNumber	int
  ImageLink  	string
}

try to fill it like this
var (
  
   	index = make(map[int]*model.Post)
  name = "TestPost"
)
  func DataIndexPage() *(map[int]*model.Post) {
  result, err := db.Query(sqlNamePost,name)
  if err != nil {
    log.Println(err)
  }

  // Slice
  posts := make([]*model.Post,0)
  imgs  := make([]*model.ImagePost,0)

  for result.Next() {
    post := new(model.Post)
    img  := new(model.ImagePost)
    err  := result.Scan(&post.ID,&post.Title,&post.Description,&post.Rating,&post.Author,&img.ImageNumber,&img.ImageLink)

    if err != nil{
      fmt.Println(err,"Gero")
      continue
    }
    imgs  = append(imgs,img)
    posts = append(posts,post)
    
  }

  
  //Cycle for
  for _, post := range posts{
    index[post.ID] = post
  }
  return &index

}

the template looks like this
{{define "postList"}}
<div class="post-list">

    {{range $key, $value:=.}}
    <div class="post" id="{{$value.ID}}">
        <div class="post-rating">
            <p>Rating:{{$value.Rating}}</p>
        </div>
        <div class="post-body">
            <div class="post-content">
                <div class="post-content-image-block">
                        //Тут я пытаюсь пробежаться по структуре ImagePost
                        {{range .Images}}
                    <img src="{{.ImageLink}}" width="600" height="550">
                        {{end}}
                    <p>Title:{{$value.Title}}</p>
                </div>
                <div class="post-content-description-block">
                    <p>Description:{{$value.Description}}
                </div>
                <div class="post-footer">
                    <div class="post-tools">

                    </div>
                    <div class="post-author">
                        Author:{{$value.Author}}
                    </div>
                </div>
            </div>
        </div>
    </div>
    {{end}}
</div>
 {{end}}

I get the error ""postList" at <.Images>: range can't iterate over " And indeed the ImagePost fields are empty, although I artificially populated the Post without a DB, and everything worked!
I've been trying for days now but it doesn't work :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2018-06-01
@Nube

I see that you are adding images to the imgs array, but I do not see that they are being added to the Images field of Post type objects.
Well, you also have the Images field is a pointer to a slice of ImagePost objects, you need to dereference it before iterating over it. Or just remove the pointer from there and do just:

type Post struct {
..........................
    Images		[]ImagePost
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question