samedi 22 juillet 2017

Golang template + Struct + html table

I'm currently studying about GoLang but I tried to figure the code below but nothing is showed in the html template, Could you clarify how can pass an array of data from go code to html file?

package main

import (

    "fmt"
    "net"
    "html/template"
    "net/http"

    "log"
)

type Data struct {
    ipaddress []string
    mailhost  []string
}

var templates *template.Template

func init() {
    //Compile templates on start
    templates = template.Must(template.ParseGlob("template/*.html"))
}

func research(w http.ResponseWriter, r *http.Request, value string) {

    record := new(Data)

    ip, err := net.LookupHost(value)

    if err != nil {
        log.Fatal("LookupHost: ", err)
    }

    mx, err := net.LookupMX(value)
    if err != nil {
        log.Fatal("LookupMX: ", err)
    }

    record.ipaddress = make([]string, len(ip))
    for i := 0; i < len(ip); i++ {
        record.ipaddress[i] = ip[i]
        fmt.Printf("IP address #%d : %s \n", i, record.ipaddress[i])
    }

    record.mailhost = make([]string, len(mx))
    for i := 0; i < len(mx); i++ {
        record.mailhost[i]= mx[i].Host
        fmt.Printf("Host : %s \n", record.mailhost[i])
    }

    display (w, "index", &record.ipaddress)
}

//Display the named template
func display(w http.ResponseWriter, tmpl string, data interface{}) {
    templates.ExecuteTemplate(w, tmpl, data)
}

func mainHandler(w http.ResponseWriter, r *http.Request) {

    r.ParseForm()
    var entryDomain = r.FormValue("entry-domain")

    if entryDomain == "" {
        display(w, "index", nil)
    } else {
        research(w, r, entryDomain)
    }


}

func main() {
    http.HandleFunc("/", mainHandler)

    err := http.ListenAndServe(":8080", nil) // setting listening port
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

and the template file is

    
<html lang="en">

<head>
    <link href="css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
    <div class="container">
        <div class="page-title">
        </div>

        <form action="/" method="POST">
            <div class="row">
                <div class="col-lg-6">
                    <div class="form-group">
                        <label for="basic-domain">User-entry domain name</label>
                        <div class="input-group">
                            <span class="input-group-addon" id="basic-addon3">e.g. dyn.com</span>
                            <input type="text" class="form-control" id="basic-domain" aria-describedby="basic-addon3" name="entry-domain" required>
                            <span class="input-group-btn">
                                <input type="submit" class="btn btn-primary pull-right" />
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </form>

        <div>
            <ol>
                
                <li></li>
                
            </ol>
        </div>


    </div>
    <script src="js/bootstrap.min.js"></script>

</body>

</html>


Thanks and I'll appreciate any help

Edson

Aucun commentaire:

Enregistrer un commentaire