在Go语言中创建一个包含未导出子结构的结构实例。

huangapple go评论74阅读模式
英文:

Creating an instance of a structure with unexported sub-structures in Go

问题

我正在尝试手动创建一个 ReactionAddedEvent 类型的实例,根据 这里,在 nlopes 的 Go Slack 库中。然而,子类型 reactionItem 是未导出的,这导致我在实例化对象时收到错误信息 ./bot_test.go:111: cannot refer to unexported name slack.reactionItem

以下是我的代码:

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

有没有办法让我使用所需的参数实例化这种类型的对象?

英文:

I am trying to manually create an instance of the type ReactionAddedEvent given here, in nlopes' Go Slack library. However, the sub-type reactionItem is unexported, which leads me to receive the error ./bot_test.go:111: cannot refer to unexported name slack.reactionItem when trying to instantiate the object.

Here is my code:

m := &slack.ReactionAddedEvent{
Item: &slack.reactionItem{
File: &slack.File{
Preview: "Test",
URLPrivate: "http://google.com",
},
},
Reaction: "white_check_mark",
}

When I remove the identifier &slack.reactionItem from line 2 in that snippet, I get instead the error: ./bot_test.go:112: missing type in composite literal, obviously.

Is there any way for me to instantiate an object of this type with the parameters I need?

答案1

得分: 3

首先,如果这里的slack指的是nlopes库,那么slack.ReactionAddedEvent结构体的Item字段不是一个指针,所以无论如何你都不能将slack.reactionItem结构体的地址存储到该字段中。其次,slack.reactionItemFile字段是一个字符串,而不是一个结构体。

第三,即使上述情况不是这样,如果类型没有被导出,但字段本身被导出,你也不能在单个字面量中组装结构体。相反,在创建结构体变量之后,你将不得不手动设置这些字段:

m := &slack.ReactionAddedEvent{Reaction: "white_check_mark"}
m.Item.File.Preview = "Test"
m.Item.File.URLPrivate = "http://google.com"

但是,如果你正在使用nlopes库,这样做是行不通的,因为File字段实际上不是一个结构体:
https://github.com/nlopes/slack/blob/master/websocket_reactions.go

第四,如果类型没有被导出,这可能是一个好迹象,表明你不应该操作该类型的对象。在这种情况下,在nlopes库中,这些结构体只用于解组和处理来自JSON消息的事件。

英文:

First, if slack here refers to the nlopes library, the slack.ReactionAddedEvent structure's Item field is not a pointer, so you can't store an address of a slack.reactionItem struct into that field anyway. Second, the File field of slack.reactionItem is a string, not a structure.

Third, even if the above isn't/weren't the case, if the types are not exported, but the fields themselves are, you can't assemble the structure in a single literal. Instead, you'll have to manually set those fields after creating the structure variable:

m := &slack.ReactionAddedEvent{Reaction: "white_check_mark"}
m.Item.File.Preview = "Test"
m.Item.File.URLPrivate = "http://google.com"

But again, if you're using the nlopes library, that won't work because the File field isn't actually a structure:
https://github.com/nlopes/slack/blob/master/websocket_reactions.go

And fourth, if the type isn't exported, that's probably a good sign that you shouldn't be manipulating objects of that type. In this case, in the nlopes library, those structures are only intended to be used for unmarshalling and then handling events from JSON messages.

huangapple
  • 本文由 发表于 2016年11月29日 05:47:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/40854104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定