package main
import ( "fmt" "net/http" "html/template")
var tmpl = template.Must(template.ParseGlob("static/*.html"))
func CheckErr(err error) { if err != nil { fmt.Println(err) }}
type Data struct { Member Tools}
type Member struct { Name,Family,Username string}
type Tools struct { Title string}
func Root(w http.ResponseWriter,r *http.Request) { d := Data{} var result []Data d.Member.Family = "alexfamily" d.Member.Username = "myalexer" d.Tools.Title = "This is the title" result = append(result,d) err := tmpl.ExecuteTemplate(w,"root",result) CheckErr(err)}
func main() { http.HandleFunc("/",Root) http.ListenAndServe(":4040",nil)}{{define "root"}}<!DOCTYPE html><html> <head> <title>{{.Tools.Title}}</title> </head> <body> <table> <tr> <th>name</th> <th>family</th> <th>username</th> </tr> {{range .}} <tr> <td>{{.Member.Name}}</td> <td>{{.Member.Family}}</td> <td>{{.Member.Username}}</td> </tr> {{end}} </table> </body></html>{{end}}Hi
Change your structure to :
type Data struct {
Member
Title string
}
type Member struct {
Name,Family,Username string
}
d.Tools.Title = "This is the title"
Change it to :
d.Title = "This is the title"
Finally you can use range for fetch title or any way:
<head>
<title>{{range .}}{{.Title}}{{end}}</title>
</head>
Other gophers in this group can provide other way to us
I hope my answer will help you
Good Luck