How to pass multiple data to Go template?

后端 未结 2 1580
自闭症患者
自闭症患者 2020-11-30 11:35

I want to pass two data objects to Go Template. One is a MongoDB query result and other is an integer array.

MongoDB Query:-

var results []User
sess,         


        
2条回答
  •  生来不讨喜
    2020-11-30 12:14

    You should define a struct populated with the database results query, then assign that struct to the Execute method.

    tmpl.Execute require a Writer interface and a struct

    type Inventory struct {
        Material string
        Count    uint
    }
    
    items := Inventory{"trouser", 1}    
    if err := GetTemplate("list").Execute(w, items); err != nil {
        // ... do your work
    }
    

提交回复
热议问题