dragon8899 2017-11-04 18:55
浏览 53
已采纳

避免在Go Lang中http.ResponseWriter等的类型重复

I'm studying Go and started a small web application.

Amazing. But I've already got the very basics.

So, what do you think would be a good documentation source for Go web application for real?

For example, now I have 15 methods that get "http.ResponseWriter" and etc as parameter (tons of repetition I mean).

I guess there's a better way of doing that.

But I don't want to start programming Go with the exact mindset (solutions) of other languages (Python, Ruby, Perl etc).

Not because it's wrong but because it can be (I don't know, that's the point) a mistake.

Here's an example:

func newStudentHandler(w http.ResponseWriter, r *http.Request) {
    p := studentPage{Title:"New Student"}
    t, _ := template.ParseFiles("new_student.html")
    t.Execute(w, p)
}

func newTeacherHandler(w http.ResponseWriter, r *http.Request) {
    p := teacherPage{Title:"New Teacher"}
    t, _ := template.ParseFiles("new_teacher.html")
    t.Execute(w, p)

}

func newClassHandler(w http.ResponseWriter, r *http.Request) {
    p := classPage{Title:"New Class"}
    t, _ := template.ParseFiles("new_class.html")
    t.Execute(w, p)
}

[]s Gio

  • 写回答

1条回答 默认 最新

  • doubi4531 2017-11-04 22:58
    关注

    This is how handlers are usually written in Go since they have to have that specific signature to be able to accept the two parameter values that you need to be able to handle a request.

    Now if you're intent on reducing the amount of typing you can certainly get around this. There are probably many ways how to do that but one that comes to mind would look something like this.

    type H struct {
        w http.ResponseWriter
        r *http.Request
    }
    
    type HF func(H)
    
    func (f HF) ServeHTTP(w http.ResponseWriter, r *http.Request) {
            f(H{w:w, r:r})
    }
    
    func (h H) newStudent() {
        p := studentPage{Title:"New Student"}
        t, _ := template.ParseFiles("new_student.html")
        t.Execute(h.w, p)
    }
    
    func (h H) newTeacher() {
        p := teacherPage{Title:"New Teacher"}
        t, _ := template.ParseFiles("new_teacher.html")
        t.Execute(h.w, p)
    
    }
    
    func (h H) newClass() {
        p := classPage{Title:"New Class"}
        t, _ := template.ParseFiles("new_class.html")
        t.Execute(h.w, p)
    }
    
    // ...
    
    http.Handle("/students", HF(H.newStudent))
    http.Handle("/teachers", HF(H.newTeacher))
    http.Handle("/classes", HF(H.newClass))
    

    Personally I wouldn't recommend to change your handlers from the standard ones because you may loose some clarity when you do that, for example other developers might be confused when reading your code and seeing a non-standard handler.

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

报告相同问题?

悬赏问题

  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决