英文:
Is there a way to cast Structs for sending over a channel
问题
在GOLANG中,是否有一种简单的方法可以将结构体转换为多态行为以跨通道传递?我试图通过一个通道发送不同版本的结构体,例如我将拥有不同类型的事件,比如一个LoginEvent。每个事件的结构体中将有不同数量的数据。
package main
import "fmt"
type Event struct {
EvtType EvtType
Username string
Data string
}
type LoginEvent struct {
Event
CallBackChannel chan *Event
}
type EvtType int
const (
Login EvtType = iota + 1
Logout
ChatMessage
Presense
BuddyList
)
func main() {
fakeOutputChan := make(chan<- *Event)
ourSrvChannel := make(chan *Event)
lg := (LoginEvent{Event{Login, "", ""}, ourSrvChannel})
fakeOutputChan <- (*Event)(&lg)
fmt.Println("Hello, playground")
}
英文:
In GOLANG is there an easy to way to cast structs for polymorphic behavior across channels? I'm trying to send different versions of a struct across one channel, so for example I'm going to have different types of Events, like a LoginEvent. Each one will have different amounts of data in the struct.
package main
import "fmt"
type Event struct {
EvtType EvtType
Username string
Data string
}
type LoginEvent struct {
Event
CallBackChannel chan *Event
}
type EvtType int
const (
Login EvtType = iota+1
Logout
ChatMessage
Presense
BuddyList
)
func main() {
fakeOutputChan := make(chan<- *Event)
ourSrvChannel := make(chan *Event)
lg := (LoginEvent{Event{Login,"",""} ,ourSrvChannel})
fakeOutputChan <- (*Event)(&lg)
fmt.Println("Hello, playground")
}
答案1
得分: 27
The idiomatic way to do is, is to use interfaces and then do a type assertion on the receiving end. Your Event
struct should ideally be an interface.
type Event interface {
// Methods defining data all events share.
}
type UserEvent struct {
Name string
}
// Define methods on *UserEvent to have it qualify as Event interface.
type LoginEvent struct {
...
}
// Define methods on *LoginEvent to have it qualify as Event interface.
Then you can define your channel to accept anything that qualifies as the Event
interface.
ch := make(chan Event)
The receiving end will receive the Event
objects and can do a type assertion to see what
concrete type underlies it:
select {
case evt := <- ch:
if evt == nil {
return
}
switch evt.(type) {
case *LoginEvent:
case *UserEvent:
....
}
}
英文:
The idiomatic way to do is, is to use interfaces and then do a type assertion on the receiving end. Your Event
struct should ideally be an interface.
type Event interface {
// Methods defining data all events share.
}
type UserEvent struct {
Name string
}
// Define methods on *UserEvent to have it qualify as Event interface.
type LoginEvent struct {
...
}
// Define methods on *LoginEvent to have it qualify as Event interface.
Then you can define your channel to accept anything that qualifies as the Event
interface.
ch := make(chan Event)
The receiving end will receive the Event
objects and can do a type assertion to see what
concrete type underlies it:
select {
case evt := <- ch:
if evt == nil {
return
}
switch evt.(type) {
case *LoginEvent:
case *UserEvent:
....
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论