0

There are two struct:

    Offers   := "10% Discunt"
    SelectUsers := "ALL"
    OpenDate    := "29-06-2020"
    closedDate   := "29-07-2020"

}

type ScanInfo struct{
    IdScan    := "1212"
    Longitude := "98.323202"
    Latitude:= "34.432201"

}

Go method:


http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        
tmpl := template.Must(template.ParseFiles("home.html"))
        var scans []ScanInfo
        var infos []info

        tmpl.Execute(w, struct{ ScanData []ScanInfo }{scans})
    })

The method which I used to get Single types of data In html table is:

 <tbody>
    {{ range .ScanData }}
       <tr>
          <th>{{.IdScan}}</th>
          <th>{{.Longitude}}</th>
          <th>{{.Latitude}}</th>
        </tr>
      {{ end }}
 </tbody>

Now I want to send both of the struct's (info & ScanInfo's) data into the HTML form (home.html) at a time. Please Suggest's me anyways to send this data, and how can I get this values into the HTML page.

artspb
  • 1,127
  • 1
  • 10
  • 19
  • Does this answer your question? [golang template with multiple structs](https://stackoverflow.com/questions/25329647/golang-template-with-multiple-structs) –  Jun 29 '20 at 17:43
  • No. This is used for single struct. But I need to sent multiple struct at a time. – Mostafijur Rahman Jun 29 '20 at 17:47

1 Answers1

1

It is easier to create a map for these cases instead of a struct:

 tmpl.Execute(w, map[string]interface{}{"ScanData":scans,"InfoData":infos})

In your template not you can use two top level variables, {{.ScanData}} and {{.InfoData}}

Burak Serdar
  • 46,455
  • 3
  • 40
  • 59