Code Monkey home page Code Monkey logo

sandwich's Introduction

sandwich

通过定义接口生成代码实现功能扩展(装饰器模式)

Example 文件说明

说明

假设需求

现有一个 Origin 对象(参考example/origin.go),需要对 Origin 对象的方法进行扩展

  1. 对 key 参数增加前缀
  2. 统计耗时

实现

  1. 定义包装代码(example/wrapper.go)
  2. 执行 go:generate 生成代码(example/wrapper_gen.go)
  3. 执行测试用例(example/wrapper_test.go) 最后执行测试代码后会输出
DoSomething1
start:2024-06-25 12:38:19.014668 +0800 CST m=+0.000469745
WrapperMethod                     # 包装方法输出  
DoSomething1, key->  prefix:key   # 改变后的入参,原来的方法输出
cost:0s                           # 包装方法输出,统计执行耗时  

DoSomething2
start:2024-06-25 12:38:19.014674 +0800 CST m=+0.000476388
WrapperMethod
DoSomething2, keys->  [prefix:key1 prefix:key2] 
cost:0s

sandwich's People

Contributors

xyz327 avatar

Stargazers

漠城 avatar  avatar 玄冰裂日 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.