I'm starting out with Go and therefore this may be a simple answer but I can't find it on the net so far.
I have the following structs:
type Answer struct {
AnswerId int
AnswerText string
Selected bool
}
type Answers struct {
answers []Answer
}
type Question struct {
QuestionId int
Answers
QuestionText string
}
This is a simple look of the domain model that backs a web app for a questionnaire.
func loadPage() (*Question, error) {
return &Question{
QuestionId: 321,
QuestionText: "What's the answer?",
Answers: Answers{
answers: []Answer{
Answer{
AnswerId: 1,
AnswerText: "Answer number 1",
Selected: false,
},
Answer{
AnswerId: 2,
AnswerText: "Answer number 2",
Selected: false,
},
},
},
}, nil
}
Here you can see that I've stubbed out a Question with a few answers. This has been stubbed just so I can send something to the view.
func viewHandler(w http.ResponseWriter, r *http.Request) {
p, _ := loadPage()
fmt.Fprintf(w, for _,element := range p.Answers.answers {
//Do something with each element in answers
})
}
This is where I'm stuck; my viewHandler
. What is the syntax that will allow me to create a checkbox group based upon the contents of my answers
slice? Any help will be gratefully received.