I have a struct with many fields (some of them are pointers to other structs as well) which are being continuosly updated in a separate goroutine. The same struct is accessed from go's http
template when a page is served.
Code example:
type SharedStruct struct {
Description string
Counter int
Status_ *Status
LastChecked time.Time
//other fields
}
var shared = &SharedStruct{}
go func() {
//..updates fields every 5 minutes
}()
go-http handler:
func someHandler(w http.ResponseWriter, r *http.Request) {
t.ExecuteTemplate(w, "page.html", shared)
}
and page.html
template:
...
Status: {{.Status_.StatusCode}}
Counter: {{.Counter}}
Last checked: {{.LastChecked.Format "2006-02-01 15:04:05"}}
So far everything works as expected, but I'm aware that bad things can happen without any synchronization. What is the preferred way to handle this properly?