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.

2
  • Does this answer your question? golang template with multiple structs Commented Jun 29, 2020 at 17:43
  • No. This is used for single struct. But I need to sent multiple struct at a time. Commented Jun 29, 2020 at 17:47

1 Answer 1

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}}

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.