我正在使用https://github.com/kataras/iris golang网络框架。这是在这里提出的最后一个问题的后续邮件- 获取登录的用户信息以进行显示-Golang模板
我最终使用的是上一篇文章中提到的代码,例如:
ctx.Values().Get("user")
并且用户设置或具有的值是“结构”类型:-
// users is struct below
var user users
// details are fetched from DB and assigned to user
// like mentioned here http://go-database-sql.org/retrieving.html
// Now value is set
ctx.Values().Set("user", user);
Run Code Online (Sandbox Code Playgroud)
但是获取值后,当我在其他处理程序中使用并打印时:-
user := ctx.Values().Get("user")
fmt.Println(user.ID)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
user.ID undefined (type interface {} is interface with no methods)
我需要接口的“类型断言”方面的帮助。我如何“键入断言”高于价值?
一个类型断言做到了这一点,断言值是给定的类型。
userID := user.(users).ID
Run Code Online (Sandbox Code Playgroud)
使用它们键入名称,它应该可以工作。