19
在GOLANG中是否有一種簡單的方法可以爲跨渠道的多態行爲投射結構?我試圖通過一個通道發送一個結構體的不同版本,所以例如我將有不同類型的事件,比如LoginEvent。每個人在結構中都有不同數量的數據。有沒有方法通過通道發送Structs
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")
}