英文:
Create Checkbox Group from Slice
问题
我正在使用Go语言开始编程,所以这可能是一个简单的问题,但是我在网上找不到答案。
我有以下的结构体:
type Answer struct {
AnswerId int
AnswerText string
Selected bool
}
type Answers struct {
answers []Answer
}
type Question struct {
QuestionId int
Answers // 匿名字段
QuestionText string
}
这是支持问卷调查的Web应用程序的领域模型的简单示例。
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
}
在这里,你可以看到我用一些答案填充了一个问题。这只是为了能够向视图发送一些内容。
func viewHandler(w http.ResponseWriter, r *http.Request) {
p, _ := loadPage()
for _, element := range p.Answers.answers {
// 根据answers切片的内容创建一个复选框组
// 对answers中的每个元素进行操作
}
}
这就是我卡住的地方,我的viewHandler
函数。请问有什么语法可以根据answers
切片的内容创建一个复选框组?非常感谢您的帮助。
英文:
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.
答案1
得分: 1
首先,这是你可以改进代码的方法:
type Answers []Answer
type Question struct {
QuestionId int
Answers Answers
QuestionText string
}
不要使用嵌入类型来表示“IS-A”关系,而是使用一个类型为Answers的属性会更合适,以避免复杂的结构定义。
现在,这是你的viewHandler
可能看起来的样子:
func ViewHandler(w http.ResponseWriter, r *http.Request) {
// 这应该从另一个模板文件中加载
const tpl = `
<!DOCTYPE html>
<html>
<body>
<form action="demo" method="POST">
<!-- 一旦pagedata结构存在于上下文中,
我们可以使用点符号查询其字段值 -->
<h3>{{.QuestionText}}</h3>
{{range .Answers}}
<input type="radio" name="" {{if .Selected}}checked{{end}}>
>{{.AnswerText}}<br>
{{end}}
</section>
</body>
</html>
`
t, _ := template.New("questionaire").Parse(tpl)
pagedata, _ := loadPage()
// 传入数据结构
_ = t.Execute(w, pagedata)
}
你只需要解析模板,然后使用Execute
方法将数据结构传递给它,以便在响应的上下文中使其数据可用。
完整的代码可以在这里查看:https://play.golang.org/p/6PbX6YsLNt
英文:
First, here is what you can do to improve the code
Type Answers []Answer
type Question struct {
QuestionId int
// Question is not an "Answers" but has "Answers"
Answers Answers
QuestionText string
}
Instead of using embedded type to represent "IS-A" relationship, having an attribute of type Answers should be more appropriate and to avoid complex struct definition.
Now here is what your viewHandler
might look like:
func ViewHandler(w http.ResponseWriter, r *http.Request) {
// This should be loaded from another template file
const tpl = `
<!DOCTYPE html>
<html>
<body>
<form action="demo" method="POST">
<!-- Once the pagedata struct exists in the context,
we can query its field value with dot notation -->
<h3>{{.QuestionText}}</h3>
{{range .Answers}}
<input type="radio" name="" {{if .Selected}}checked{{end}}
>{{.AnswerText}}<br>
{{end}}
</section>
</body>
</html>
`
t, _ := template.New("questionaire").Parse(tpl)
pagedata, _ := loadPage()
// Pass in the data struct
_ = t.Execute(w, pagedata)
}
You just have to parse the template, then with Execute
pass the data struct you want its data available in the context of the response.
See the full code here https://play.golang.org/p/6PbX6YsLNt
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论