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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Efficiently handle concurrency security issues in multi-process log writing. Multiple processes write the same log file at the same time. How to ensure concurrency is safe and efficient? This is a...
