Golang if else in HTML templates tutorial #7

Golang if else in HTML templates tutorial #7

 

main.go Source Code file

package main
import (
    "html/template"
    "net/http"
)
func main() {
    type Student struct {
        Name     string
        Class    int
        Reg      int
        Promoted bool
    }
    data := Student{
        Name:     "khan",
        Class:    12,
        Reg:      232,
        Promoted: false,
    }
    tmpl, _ := template.ParseGlob("templates/*.html")
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        tmpl.ExecuteTemplate(w, "index.html", data)
    })
    http.ListenAndServe(":8080", nil)
}

HTML TEMPLATE Source Code file

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GOLANG INDEX HTML</title>
</head>
<body>
    <h1>Student Result and Promotion</h1>
    {{if .Promoted}}
        congrats {{.Name}} from {{.Class}} and {{.Reg}}
    {{else}}
        failed {{.Name}} from {{.Class}} and {{.Reg}}
    {{end}}
</body>
</html>

 

 

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: