当前位置: 首页 > news >正文

用vs2010里的vb做网站seo网络培训班

用vs2010里的vb做网站,seo网络培训班,网页模板下载了如何修改使用?,想找公司做网站前言 我一直以来对golang的装饰器模式情有独衷,不是因为它酷,而是它带给我了太多的好处。首先我不想说太多的概念,熟记这些概念对我的编程来说一点用处没有。我只知道它给我带来了好处,下面谈谈我的理解。 这种模式可以很轻松地…

前言

我一直以来对golang的装饰器模式情有独衷,不是因为它酷,而是它带给我了太多的好处。首先我不想说太多的概念,熟记这些概念对我的编程来说一点用处没有。我只知道它给我带来了好处,下面谈谈我的理解。

这种模式可以很轻松地把一些函数装配到另外一些函数上,让你的代码更加简单,也可以让一些“小功能型”的代码复用性更高,让代码中的函数可以像乐高玩具那样自由地拼装。

重要的是你不用修改代码以前的功能,对以前的功能没有影响,而是动态的,很方便的扩展函数的功能。下面我将举几个例子说明下

golang 的装饰器通常用interface{} 以及 anonymous functions 实现的,下面我们看看实际的例子

一、interface{} 实现装饰器

type Printer interface {Print() string
}
​
type SimplePrinter struct {}
​
func (sp *SimplePrinter) Print() string {return "Hello, world!"
}
​
func BoldDecorator(p Printer) Printer {return PrinterFunc(func() string {return "<b>" + p.Print() + "</b>"})
}
​
type PrinterFunc func() string
​
func (pf PrinterFunc) Print() string {return pf()
}
​
func main() {simplePrinter := &SimplePrinter{}boldPrinter := BoldDecorator(simplePrinter)fmt.Println(simplePrinter.Print()) // Output: Hello, world!fmt.Println(boldPrinter.Print()) // Output: <b>Hello, world!</b>
}
  1. 在上面的代码中我们定义了一个Printer接口,一个 SimplePrinter 结构体实现了Print方法

  2. 我们定义了 BoldDecorator 函数接受一个Printer接口返回一个Printer接口.该函数将原来的 Print() 方法封装到一个新的方法中,该方法返回的是用 <b> 标记括起来的相同值

  3. 这只是一个简单的例子,却展示了装饰器的强大的功能。通过添加新的装饰器,我们可以在运行时改变对象的行为,而无需更改其原始代码。当我们需要为一个已经存在的对象添加新功能,而又想保持其原始代码不变时,装饰器模式就显得尤为有用。这样,我们就可以避免为每一个想要添加的新功能创建新的子类。

二、Http 相关的装饰器的例子

func WithServerHeader(h http.HandlerFunc) http.HandlerFunc {return func(writer http.ResponseWriter, request *http.Request) {writer.Header().Set("server", "0.01")h(writer, request)}
}
​
func withServerSetCook(h http.HandlerFunc) http.HandlerFunc {return func(writer http.ResponseWriter, request *http.Request) {cookie := http.Cookie{Name: "username", Value: "tt"}http.SetCookie(writer, &cookie)h(writer, request)}
}
​
func WithBasicAuth(h http.HandlerFunc) http.HandlerFunc {return func(writer http.ResponseWriter, request *http.Request) {cookie, err := request.Cookie("username")if err != nil || cookie.Value != "ee" {writer.WriteHeader(http.StatusForbidden)return}h(writer, request)}
}
​
func WithDebugLog(h http.HandlerFunc) http.HandlerFunc {return func(writer http.ResponseWriter, request *http.Request) {request.ParseForm()log.Println(request.Form)log.Println("path", request.URL.Path)log.Println("Host", request.URL.Host)log.Println(request.Form["url_long"])for k, v := range request.Form {log.Println("key:", k)log.Println("value:", v)}h(writer, request)}
}
​
func hello(w http.ResponseWriter, r *http.Request) {log.Printf("Recieved Request %s from %s\n", r.URL.Path, r.RemoteAddr)fmt.Fprintf(w, "Hello, World! "+r.URL.Path)
}
​
func main() {http.HandleFunc("/hello/v1", WithServerHeader(hello))http.HandleFunc("/hello/v2", withServerSetCook(hello))http.HandleFunc("/hello/v3", WithBasicAuth(hello))http.HandleFunc("/hello/v4", WithDebugLog(hello))err := http.ListenAndServe(":8080", nil)if err != nil {log.Fatal("ListenAndServe: ", err)}
}
  1. 例子中 WithServerHeader,withServerSetCook,WithBasicAuth, WithDebugLog 就是一个装饰器,它传入一个 http.HandlerFunc 就是一个改写版本。而我们的业务hello不用修改任何功能,可以呈现出一些新的功能,很多人把这种模式称为middleware ,我更喜欢称为装饰器

三、多个装饰器Pipeline,也是options 模式

type googleSlide struct {sreSlide *list.Listinterval int64mutex    sync.Mutexk        int64
}type slideVal struct {time   int64req    int64accept int64
}type SlideValOptions func(val *slideVal)func NewSlideval(options ...SlideValOptions) *slideVal {t := &slideVal{time: time.Now().UnixNano(),}for _, option := range options {option(t)}return t
}func WithReqOption(req int64) SlideValOptions {return func(val *slideVal) {val.req = req}
}func WithAcceptReqOption(accept int64) SlideValOptions {return func(val *slideVal) {val.accept = accept}
}func NewGoogleSlide(interval time.Duration, k int64) *googleSlide {return &googleSlide{sreSlide: list.New(),interval: interval.Nanoseconds(),k:        k,}
}func (g *googleSlide) Sre() grpc.UnaryClientInterceptor {return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {g.mutex.Lock()now := time.Now().UnixNano()front := g.sreSlide.Front()for front != nil && front.Value.(*slideVal).time+g.interval < now {g.sreSlide.Remove(front)front = g.sreSlide.Front()}var r, accept int64front = g.sreSlide.Front()for front != nil {t := front.Value.(*slideVal)r += t.reqaccept += t.acceptfront = front.Next()}tail := (float64(r) - float64(g.k*accept)) / (float64(r) + 1)if tail > 0 {g.mutex.Unlock()return errors.New("request is fail")}g.sreSlide.PushBack(NewSlideval(WithReqOption(1)))err := invoker(ctx, method, req, req, cc, opts...)if err == nil {g.sreSlide.PushBack(NewSlideval(WithAcceptReqOption(1)))}g.mutex.Unlock()return err}
}

这个代码是我在SRE 熔断器写的代码,现在重新拿出来,是因为具有代表性 。NewSlideval 可以支持多个装饰器,遍历装饰器,就可以得倒我们想要的功能,只要我们去实现这个装饰器。这样的代码,在golang 是常用的,在初始化配置函数经常用

http://www.ritt.cn/news/3689.html

相关文章:

  • 英语网站建设宁波网站seo诊断工具
  • 曰本孕妇做爰网站个人网页制作教程
  • wordpress怎么做企业网站信息流优化师简历模板
  • 互联网定制网站热点军事新闻
  • 徐州建站seo教学免费课程霸屏
  • 网站建设要准备什么百度小说搜索风云榜排名
  • 个人网站备案 服务内容怎么写百度推广客户端怎么登陆
  • 美观网站建设物美价廉seo关键词优化价格
  • 做网站首页ps中得多大百度快照客服电话
  • 济南专业做网站的公司哪家好百度指数代表什么
  • 个人是否可以做网站百家号排名
  • 有源码怎么做网站站长之家是什么
  • 手机网站报名链接怎么做网站推广方式组合
  • 洛阳凯锦腾网业有限公司百度seo引流怎么做
  • 中企动力网站建设 医疗网络服务是什么
  • 手机网站建设的公司怎么在平台上做推广
  • 网站公安备案查询系统seo网站有优化培训吗
  • 做分色找工作网站百度公司介绍
  • 网站图片像素手把手教你优化网站
  • 免费做自荐书的网站seo外包公司兴田德润官方地址
  • 个人网站需要备案吗seo黑帽是什么
  • 工程项目网站太原百度seo排名软件
  • 做外贸的物流网站北京优化网站公司
  • 怎么做百度自己的网站空间培训机构专业
  • 国际外贸网站推广nba排名
  • 创意品牌型网站seo培训公司
  • wordpress医疗模板下载seo推广有哪些
  • 中山市政府网站建设广州网站优化服务
  • p2vr做的网站上传信息流优化师怎么入行
  • 网站的链接优化百度小说风云榜排名