Golang Passing Complex data to HTML tutorial # 6

Golang Passing Complex data to HTML tutorial # 6


Golang File

<div>package main</div>
<div>import (</div>
<div>    "html/template"</div>
<div>    "net/http"</div>
<div>func main() {</div>
<div>    type productSpecs struct {</div>
<div>        Size   float64</div>
<div>        Weight float64</div>
<div>        Desc   string</div>
<div>    }</div>
<div>    type product struct {</div>
<div>        Category string</div>
<div>        Name     string</div>
<div>        Price    float64</div>
<div>        Date     string</div>
<div>        SKU      string</div>
<div>        Specs    productSpecs</div>
<div>    }</div>
<div>    data := product{</div>
<div>        Category: "Health",</div>
<div>        Name:     "Tuber",</div>
<div>        Price:    22.22,</div>
<div>        Date:     "02-02-3029",</div>
<div>        SKU:      "32",</div>
<div>        Specs: productSpecs{</div>
<div>            Size:   22.0,</div>
<div>            Weight: 30.0,</div>
<div>            Desc:   "Here is my product desc",</div>
<div>        },</div>
<div>    }</div>
<div>    tmpl, _ := template.ParseGlob("templates/*.html")</div>
<div>    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {</div>
<div>        tmpl.ExecuteTemplate(w, "index.html", data)</div>
<div>    })</div>
<div>    http.ListenAndServe(":8080", nil)</div>


<div>&lt;html lang="en"&gt;</div>
<div>    &lt;meta charset="UTF-8"&gt;</div>
<div>    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;</div>
<div>    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;</div>
<div>    &lt;title&gt;GOLANG INDEX HTML&lt;/title&gt;</div>
<div>    &lt;h1&gt;INDEX HTML PAGE&lt;/h1&gt;</div>
<div>    Category &lt;b&gt;{{.Category}}&lt;/b&gt;&lt;br/&gt;</div>
<div>    Name &lt;b&gt;{{.Name}}&lt;/b&gt;&lt;br/&gt;</div>
<div>    Price &lt;b&gt;{{.Price}}&lt;/b&gt;&lt;br/&gt;</div>
<div>    Date &lt;b&gt;{{.Date}}&lt;/b&gt;&lt;br/&gt;</div>
<div>    SKU &lt;b&gt;{{.SKU}}&lt;/b&gt;&lt;br/&gt;</div>
<div>    Specs-&gt; &lt;br/&gt;</div>
<div>    Size &lt;b&gt;{{.Specs.Size}}&lt;/b&gt;&lt;br/&gt;</div>
<div>    Weight &lt;b&gt;{{.Specs.Weight}}&lt;/b&gt;&lt;br/&gt;</div>
<div>     Desc &lt;b&gt;{{.Specs.Desc}}&lt;/b&gt;&lt;br/&gt;</div>
Tony BB

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: