duanjie2940 2015-11-15 21:44
浏览 5
已采纳

从切片创建复选框组

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条回答 默认 最新

  • dongyi7669 2015-11-15 22:02
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料