簡體   English   中英

類型 interface {} 是沒有方法的接口

[英]Type interface {} is interface with no methods

我正在使用https://github.com/kataras/iris golang web 框架。 這是來自此處提出的最后一個問題的后續郵件 - 獲取登錄的用戶信息以進行顯示 - 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);

但是在獲得值后,當我在不同的處理程序中使用並打印時:-

user := ctx.Values().Get("user")
fmt.Println(user.ID)

我收到錯誤:-

user.ID undefined (type interface {} is interface with no methods)

我需要接口的“類型斷言”方面的幫助。 如何在值上方“輸入斷言”?

類型斷言就是這樣做的,斷言一個值是給定的類型

userID := user.(users).ID

使用他們輸入名稱,它應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM