zoukankan      html  css  js  c++  java
  • go的web框架的context回调的原理

    package main
    
    import "fmt"
    
    type ctx struct {
    	name string
    	idx  int8
    	h    handlers
    }
    
    /*
    *
    idx=0 func1 start
    	idx = 1 func2 start
    		idx = 2 func3 start
    		idx = 3 return
    	idx =1 func2 end
    idx=0 func1 end
    *
    */
    func (c *ctx) next() {
    	c.idx++
    	fmt.Printf("idx:%d
    ", c.idx)
    	for c.idx < int8(len(c.h)) {
    		c.h[c.idx](c)
    		c.idx++
    	}
    }
    
    type handler func(*ctx)
    
    type handlers []handler
    
    type engin struct {
    	name string
    	hds  handlers
    }
    
    func (e *engin) run(c *ctx) {
    	fmt.Printf("engine:%s running
    ", e.name)
    	c.h = e.hds
    	c.next()
    	fmt.Printf("engine:%s end
    ", e.name)
    }
    
    func main() {
    	testHandler()
    }
    
    func testHandler() {
    
    	h1 := func(c *ctx) {
    		fmt.Println("h1 start")
    		c.next()
    		fmt.Println("h1 end")
    	}
    
    	h2 := func(c *ctx) {
    		fmt.Println("h2 start")
    		c.next()
    		fmt.Println("h2 end")
    	}
    
    	h3 := func(c *ctx) {
    		fmt.Println("h3 running...")
    	}
    
    	hds := make([]handler, 3)
    	hds[0] = h1
    	hds[1] = h2
    	hds[2] = h3
    
    	c1 := &ctx{
    		idx:  -1,
    		name: "ctx1",
    	}
    
    	engin1 := &engin{
    		name: "engin1",
    		hds:  hds,
    	}
    	engin1.run(c1)
    
    }  

    结果

    engine:engin1 running
    idx:0
    h1 start
    idx:1
    h2 start
    idx:2
    h3 running...
    h2 end
    h1 end
    engine:engin1 end
    

      

  • 相关阅读:
    c++常用库
    boost
    android
    UITableView 多选
    c++ 比较两个集合
    事件加不上的另一种原因
    ios多线程
    ubuntu android
    jna StdCallCallback 回调问题查证
    java
  • 原文地址:https://www.cnblogs.com/beckbi/p/12902381.html
Copyright © 2011-2022 走看看