我創建了一個結構,這個結構中包含兩個列表類型。當我嘗試實例化我的結構時,我收到錯誤cannot use list.New() (type *list.List) as type list.List in field value我正在使用 golang 游樂場結構type myStruct struct { name string messages list.List users list.List lastUsed time.Time}實例化結構var myVar = myStruct{"hello", list.New(), list.New(), time.Now()}
2 回答

哆啦的時光機
TA貢獻1779條經驗 獲得超6個贊
list.New() 返回一個指針*List,而 myStruct 將其字段聲明為List。
func New() *列表
消息和用戶應該是 *list.List
type myStruct struct {
name string
messages *list.List
users *list.List
lastUsed time.Time
}
根據您的需要的另一種方法,您可以按如下方式初始化結構:
var myVar = myStruct{"hello", *list.New(), *list.New(), time.Now()}
- 2 回答
- 0 關注
- 202 瀏覽
添加回答
舉報
0/150
提交
取消