简体   繁体   中英

Golang How to template a nested struct?

I'm trying to template a JSON response onto a front end, my current struct is this:

    type Response struct {
    WhoisRecord struct {
        CreatedDate time.Time `json:"createdDate"`
        UpdatedDate time.Time `json:"updatedDate"`
        ExpiresDate time.Time `json:"expiresDate"`
        Registrant  struct {
            Name         string `json:"name"`
            Organization string `json:"organization"`
            Street1      string `json:"street1"`
            City         string `json:"city"`
            State        string `json:"state"`
            Country      string `json:"country"`
            CountryCode  string `json:"countryCode"`
            Email        string `json:"email"`
            Telephone    string `json:"telephone"`
            Fax          string `json:"fax"`
        } `json:"registrant"`
        AdministrativeContact struct {
            Name         string `json:"name"`
            Organization string `json:"organization"`
            Street1      string `json:"street1"`
            City         string `json:"city"`
            State        string `json:"state"`
            Country      string `json:"country"`
            CountryCode  string `json:"countryCode"`
            Email        string `json:"email"`
            Telephone    string `json:"telephone"`
            Fax          string `json:"fax"`
        } `json:"administrativeContact"`
        TechnicalContact struct {
            Name         string `json:"name"`
            Organization string `json:"organization"`
            Street1      string `json:"street1"`
            City         string `json:"city"`
            State        string `json:"state"`
            Country      string `json:"country"`
            CountryCode  string `json:"countryCode"`
            Email        string `json:"email"`
            Telephone    string `json:"telephone"`
            Fax          string `json:"fax"`
        } `json:"technicalContact"`
        DomainName  string `json:"domainName"`
        NameServers struct {
            HostNames []string      `json:"hostNames"`
        } `json:"nameServers"`
        RegistrarName         string `json:"registrarName"`
        Ips                []string `json:"ips"`
    } `json:"WhoisRecord"`
}

I then unmarshal this json response, and pass it onto the front end (I'm using GIN)

( Response is redeclared as res )

c.HTML(200,"homework.html", gin.H{
        "whois":    res,
    })

But this is where i run into problems, the code works, but i'm not sure how to template it since it's nested.

For example, i want to show Registrant, Administrative, and Technical contact details (all fields returned) into separate tables. Along with displaying the created, updated, and expired dates. Then Finalizing by showing the The registrar, the ips, and the nameservers (in this case the hostnames field under the NameServers )

How would i go about serving this in my homework.html file? I've tried everything. Usually i would just do something like:

To Display IPs:

                                        {{ range .Ips }}
                                            <div>
                                                <p>IP</p>
                                                <h6>{{ . }}</h6>
                                            </div>
                                        {{end}}

To Display Register Data:

                                        <div>
                                            <p>Name</p>
                                            <h6>{{ .Name }}</h6>
                                            <p>Email</p>
                                            <h6>{{ .Email }}</h6>
                                            //etc
                                        </div>

To display the Registrar:

                                            <div>
                                                <p>Registrar</p>
                                                <h6>{{ .RegistrarName }}</h6>
                                            </div>

But none of this is working (How do i display registrant name in one field, then display technical name in another? i'm obviously messing up the templates big time and my understanding of it is a bit skewed). I've read everything i could, i tried to divide the structs and serve as separate structs, etc. Nothing is working. Can someone point me in the right direction and give examples?

Thanks!

Templates fields are evaluated relative to the current context {{.}}. The following evaluates the template using a map containing "whois" as key, and res as the value:

in.H{ "whois": res})

The value of {{.whois}} is your struct, and you can access the struc field from there. So you can do:

{{.whois.Registrant.Name}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM