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 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输