我正在嘗試手動創建一個類型爲ReactionAddedEvent的實例,並給出here, in nlopes' Go Slack library。但是,子類型reactionItem是未導出的,導致我在嘗試實例化對象時收到錯誤./bot_test.go:111: cannot refer to unexported name slack.reactionItem
。在Go中創建一個包含未導出子結構的實例
這裏是我的代碼:
m := &slack.ReactionAddedEvent{ Item: &slack.reactionItem{ File: &slack.File{ Preview: "Test", URLPrivate: "http://google.com", }, }, Reaction: "white_check_mark", }
當我在那個片段,從第2行中刪除標識&slack.reactionItem
,我得到的,而不是錯誤:./bot_test.go:112: missing type in composite literal
,效果顯着。
有什麼辦法讓我用我需要的參數實例化這種類型的對象嗎?
爲什麼該軟件包會首先導出ReactionAddedEvent? – mpm
它是未導出結構的typedef,但它看起來像導出類型(帶有導出字段),因此庫的客戶端可以爲事件類型實現處理程序。從他們的示例字段:https://github.com/nlopes/slack/blob/master/examples/websocket/websocket.go – Kaedys
然而,這個想法似乎是事件是由鬆散庫創建_create_事件作爲JSON消息接收,然後轉換爲客戶端可以爲其設置處理程序的這些導出類型事件。客戶不應該自己創建事件。 – Kaedys