March 2020
Intermediate to advanced
406 pages
8h 39m
English
We can also use HTML templating, akin to how we performed text tempting, in order to generate dynamic results for HTML pages in Go. In order to do this, we need to initialize our package, import the proper dependencies, and set up a data structure to hold the values that we are planning to use in our HTML templates, like so:
package main import ( "html/template" "net/http") type UserFields struct { Name string URL string Email string}
Next, we create the userResponse HTML template:
var userResponse = ` <html><head></head><body><h1>Hello {{.Name}}</h1><p>You visited {{.URL}}</p><p>Hope you're enjoying this book!</p> <p>We have your email recorded as {{.Email}}</p></body></html>`
Then, we create an HTTP request handler:
func ...
Read now
Unlock full access