Comments (2)
// 带方法的 interface
type iface struct {
tab *itab
data unsafe.Pointer
}
// 不带方法的 interface
type eface struct {
_type *_type
data unsafe.Pointer
}
Golang interface 类型底层实现中存在两种指针,类型指针和值指针,只有当两者都为 nil 的时候,interface 类型的指针才会判断为 nil。
var a1 *a
b := test{t1:a1}
上述这段代码中,为 t1 test1 interface
赋予了一个类型指针,所以 b.isT1() = false
会成立
而this.t1.(*a) == nil
代码中断言了具体的类型再进行判断,判断 nil 的条件就变为判断值指针即可。
在 tokenService.tokenEnhancer != nil
的判断中,TokenEnhancer interface
在之前没有赋予过任何的类型指针,此时仅需判断值指针是否存在即可。
确实有可能会出现 tokenService.tokenEnhancer
被赋予了类型指针导致其被判断不为 nil 的情况,合理的判断方式应该使用反射判断 interface 是否为 nil 或者在具体实现方法中添加判断,比如:
func (enhancer *JwtTokenEnhancer) Enhance(oauth2Token *OAuth2Token, oauth2Details *OAuth2Details) (*OAuth2Token, error) {
// 此时 enhancer 被指定了具体的类型,可以直接判断
if enhancer == nil{
return nil, errors.New("enhancer is nil")
}
return enhancer.sign(oauth2Token, oauth2Details)
}
最后非常感谢您的指教,欢迎继续与我们反馈和交流!
from micro-go-book.
理解了,感谢大佬解答疑惑
from micro-go-book.
Related Issues (13)
- 勘误:反射 HOT 1
- consulClient.instancesMap.Load(serviceName) 报错代码没有实现Load()方法和Store()...等 HOT 1
- 令牌实现
- ch13 书上代码与github代码不一致
- 表格计算过程有误
- 书评 HOT 1
- 最后一章秒杀系统的代码没放进来吗? HOT 1
- 项目运行 HOT 1
- sk-admin中endpoint问题
- 注册服务失败 HOT 9
- Gin中间件问题 HOT 1
- 11章代码问题
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from micro-go-book.