有没有什么方法可以用 Go 语言进行面向切面编程 [英] Is there any method to doing Aspect-Oriented Programming in Go language

查看:97
本文介绍了有没有什么方法可以用 Go 语言进行面向切面编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在go程序中使用面向切面编程,例如在服务中记录信息,不侵入我的功能代码,但我发现没有方法可以做到.

I'm trying to use Aspect-Oriented Programming in go program, For example, log the info in service, Without intrusion to my function codes, But I find there is not a method to do.

推荐答案

Go 通常倾向于低魔法"的设计和代码,更喜欢明确性.中间件是一种常见的设计模式,它提供了很多人用 AOP 做的事情,就像你提到的简单例子一样——当你的服务器代码设置正确时,你可以注册中间件来自动记录事情等.有一些代码示例对于 Go Web Examples 站点 上的中间件,谷歌搜索会发现更多.本教程讨论日志中间件等.

Go generally leans towards design and code low on "magic", preferring explicitness. Middleware is a common design pattern that provides a lot of what folks do with AOP, like the simple example you mention - when your server code is set up properly, you can register middleware that will automatically log things, etc. There are some code samples for middleware on the Go Web Examples site, and googling will uncover much more. This tutorial talks about logging middleware, etc.

这篇关于有没有什么方法可以用 Go 语言进行面向切面编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆