b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

ruby go语言_ruby语言教程_ruby语言好学吗(2)

电脑杂谈  发布时间:2017-01-24 17:19:56  来源:网络整理

到达预期膨胀的顶点之后,你会遇到理想幻灭的低谷。ruby go语言你开始想念语言Y的特性X,此时你还没有完全的掌握地道的Go。你还在用其他编程语言的风格来写Go语言的程序,你甚至开始觉得沮丧。你可能在大量使用reflect和unsafe这两个包,但这不是地道的Go。地道的Go不会使用那些魔法一样的东西。

这个探索阶段产生的项目的一个很好的例子就是Martini Web框架。Martini是一个Go语言的早期Web框架,它从Ruby的Web框架当中吸收了很多思想(比如依赖注入)。最初,这个框架在社区中引起了强烈的反响,但是它逐渐在性能和可调试性上受到了一些批评。Martini框架的作者Jeremy Saenz积极响应这些来自Go社区的反馈,写了一个更加符合Go语言规范的库Negroni

func (m *Martini) RunOnAddr(addr string) {
    // TODO: Should probably be implemented using a new instance of http.Server in place of
    // calling http.ListenAndServer directly, so that it could be stored in the martini struct for later use.
    // This would also allow to improve testing when a custom host and port are passed.
 
    logger := m.Injector.Get(reflect.TypeOf(m.logger)).Intece().(*log.Logger)
    logger.Printf("listening on %s (%s)\n", addr, Env)
    logger.Fatalln(http.ListenAndServe(addr, m))}

来自Martini框架的交互式代码片段,它是不地道的Go的例子。注意用反射包实现的依赖注入

func TestNegroniServeHTTP(t *testing.T) {
    result := ""
    response := httptest.NewRecorder()
 
    n := New()
    n.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
        result += "foo"
        next(rw, r)
        result += "ban"
    }))
    n.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
        result += "bar"
        next(rw, r)
        result += "baz"
    }))
    n.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
        result += "bat"
        rw.WriteHeader(http.StatusBadRequest)
    }))
 
    n.ServeHTTP(response, (*http.Request)(nil))
 
    expect(t, result, "foobarbatbazban")
    expect(t, response.Code, http.StatusBadRequest)}


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-28542-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 田苗苗
      田苗苗

      于是俺们厂里买了好多跟废品差不多的浙商设备

    热点图片
    拼命载入中...