Home > Article > Backend Development > Does go language not support aop?
Go language supports aop. AOP refers to aspect-oriented programming, which is a technology that achieves unified maintenance of program functions through pre-compilation and dynamic agents during runtime; AOP is an object-oriented approach, with main application scenarios: logging, performance statistics, security control , transaction processing, exception handling, etc.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
What is aop?
In the software industry, AOP is the abbreviation of Aspect Oriented Programming, which means: Aspect-oriented programming, a technology that achieves unified maintenance of program functions through pre-compilation and dynamic agents during runtime . AOP is the continuation of OOP, a hot spot in software development, an important content in the Spring framework, and a derivative paradigm of functional programming. AOP can be used to isolate various parts of the business logic, thereby reducing the coupling between the various parts of the business logic, improving the reusability of the program, and improving the efficiency of development.
Aspect-oriented programming is just a method in object-oriented. Dynamically embedding other code during code execution is called aspect-oriented programming. Common usage scenarios:
Log
Things
Database Operation
Aspect-oriented programming is to encapsulate cross-business logic into aspects and use the functions of AOP to weave aspects into the main business logic. The so-called cross-business logic refers to general code that has nothing to do with the main business logic, such as security checks, things, logs, etc. If AOP is not used, code entanglement will occur, that is, cross-business logic is mixed with main business logic. In this way, the business logic will become confusing.
Main application scenarios: logging, performance statistics, security control, transaction processing, exception handling, etc.
Core Concept
JoinPoint: Connection point. It is a precise execution point in program execution, such as a method in a class.
PointCut: entry point. Specify which components and which methods use aspect components.
Advice: Notification, used to specify the location of specific effects, before or after the method, etc., divided into pre-notification, post-notification, exception notification, return notification, and surrounding notification .
Aspect: Aspect. Components that encapsulate common business logic, that is, the code content we want to insert.
The inherent design pattern is the proxy pattern.
Does the go language support aop?
go language supports aop.
Example of Go implementation of AOP:
// User type User struct { Name string Pass string } // Auth 验证 func (u *User) Auth() { // 实际业务逻辑 fmt.Printf("register user:%s, use pass:%s\n", u.Name, u.Pass) } // UserAdvice type UserAdvice interface { // Before 前置通知 Before(user *User) error // After 后置通知 After(user *User) } // ValidatePasswordAdvice 用户名验证 type ValidateNameAdvice struct { } // ValidatePasswordAdvice 密码验证 type ValidatePasswordAdvice struct { MinLength int MaxLength int } func (ValidateNameAdvice) Before(user *User) error { fmt.Println("ValidateNameAdvice before") if user.Name == "admin" { return errors.New("admin can't be used") } return nil } func (ValidateNameAdvice) After(user *User) { fmt.Println("ValidateNameAdvice after") fmt.Printf("username:%s validate sucess\n", user.Name) } // Before 前置校验 func (advice ValidatePasswordAdvice) Before(user *User) error { fmt.Println("ValidatePasswordAdvice before") if user.Pass == "123456" { return errors.New("pass isn't strong") } if len(user.Pass) > advice.MaxLength { return fmt.Errorf("len of pass must less than:%d", advice.MaxLength) } if len(user.Pass) < advice.MinLength { return fmt.Errorf("len of pass must greater than:%d", advice.MinLength) } return nil } func (ValidatePasswordAdvice) After(user *User) { fmt.Println("ValidatePasswordAdvice after") fmt.Printf("password:%s validate sucess\n", user.Pass) } // UserAdviceGroup,通知管理组 type UserAdviceGroup struct { items []UserAdvice } // Add 注入可选通知 func (g *UserAdviceGroup) Add(advice UserAdvice) { g.items = append(g.items, advice) } func (g *UserAdviceGroup) Before(user *User) error { for _, item := range g.items { if err := item.Before(user); err != nil { return err } } return nil } // After func (g *UserAdviceGroup) After(user *User) { for _, item := range g.items { item.After(user) } } // UserProxy 代理,也是切面 type UserProxy struct { user *User } // NewUser return UserProxy func NewUser(name, pass string) UserProxy { return UserProxy{user:&User{Name:name, Pass:pass}} } // Auth 校验,切入点 func (p UserProxy) Auth() { group := UserAdviceGroup{} group.Add(&ValidatePasswordAdvice{MaxLength:10, MinLength:6}) group.Add(&ValidateNameAdvice{}) // 前置通知 if err := group.Before(p.user); err != nil { panic(err) } // 实际逻辑 p.user.Auth() // 后置通知 group.After(p.user) }
Use AOP mode for decoupling and separation of main business and secondary business. In fact, that's it.
【Related recommendations: Go video tutorial, Programming teaching】
The above is the detailed content of Does go language not support aop?. For more information, please follow other related articles on the PHP Chinese website!