Inheritance 使用混合结构类型的Go贴图和切片
我试图通过创建一个简单的事件存储库来学习如何使用Go,该存储库能够创建投影。我被困在如何处理包含混合类型结构的切片和贴图上。这样做的要点是,我希望开发人员创建尽可能多的结构来实现各种字段所需的IEntity和IEvent 我来自JavaScript/Node.js背景,对C/C++/Java有一些基本的了解,我可能希望在这里了解更多的类/继承模式,并且需要一些关于如何在Go中获得相同功能的帮助Inheritance 使用混合结构类型的Go贴图和切片,inheritance,go,data-structures,struct,interface,Inheritance,Go,Data Structures,Struct,Interface,我试图通过创建一个简单的事件存储库来学习如何使用Go,该存储库能够创建投影。我被困在如何处理包含混合类型结构的切片和贴图上。这样做的要点是,我希望开发人员创建尽可能多的结构来实现各种字段所需的IEntity和IEvent 我来自JavaScript/Node.js背景,对C/C++/Java有一些基本的了解,我可能希望在这里了解更多的类/继承模式,并且需要一些关于如何在Go中获得相同功能的帮助 package main import ( "sync" "time" u
package main
import (
"sync"
"time"
uuid "github.com/satori/go.uuid"
)
// IEntity describes an entity, a struct that is the sum of all events resolved in a chronological order
type IEntity interface {
}
// IProjection describes the interface for a projection struct
type IProjection interface {
Lock()
Unlock()
Get(id uuid.UUID) IEntity
Set(id uuid.UUID, entity IEntity)
GetLastEventTime() time.Time
Append(event IEvent)
}
// IEvent describes the interface for a event, any event added to the system needs to implement this interface
type IEvent interface {
Resolve(projection IProjection)
}
// EventVault is the base struct that keeps all the events and allows for projections to be created
type EventVault struct {
sync.Mutex
events []IEvent
}
// Append adds a IEvent to the projection and runs that events IEvent.Resolve method
func (vault *EventVault) Append(event IEvent) {
vault.Lock()
defer vault.Unlock()
vault.events = append(vault.events, event)
}
// Project creates a projection with entities from all events up until the choosen end time
func (vault *EventVault) Project(endTime time.Time, projection IProjection) IProjection {
lastEventTime := projection.GetLastEventTime()
for index := range vault.events {
event := vault.events[index]
if event.Time.After(lastEventTime) && event.Time.Before(endTime) {
projection.Append(event)
}
}
return projection
}
// Projection caculates and stores a projection of the events appended to it with the Append method
type Projection struct {
sync.Mutex
events []IEvent
entities map[uuid.UUID]IEntity
}
// Get returns an IEntity struct from an id (for use in the IEvent.Resolve method)
func (projection *Projection) Get(id uuid.UUID) IEntity {
return projection.entities[id]
}
// Set add a IEntity to the projection (for use in the IEvent.Resolve method)
func (projection *Projection) Set(id uuid.UUID, entity IEntity) {
projection.entities[id] = entity
}
// GetLastEventTime returns the time for the event that was added last
func (projection *Projection) GetLastEventTime() time.Time {
event := projection.events[len(projection.events)-1]
if event == nil {
return time.Unix(0, 0)
}
return event.Time
}
// Append adds a IEvent to the projection and runs that events IEvent.Resolve method
func (projection *Projection) Append(event IEvent) {
projection.Lock()
projection.events = append(projection.events, event)
event.Resolve(projection)
projection.Unlock()
}
// ------------------ Below is usage of above system ------------------
// PlayerEntity is a sample entity that can be used for testing
type PlayerEntity struct {
ID uuid.UUID
Name string
Birthday time.Time
UpdatedAt time.Time
}
// AddPlayerEvent is a sample event that can be used for testing
type AddPlayerEvent struct {
ID uuid.UUID
Time time.Time
Name string
}
// Resolve will create a new PlayerEntity and add that to the projection
func (event *AddPlayerEvent) Resolve(projection IProjection) {
player := PlayerEntity{ID: uuid.NewV4(), Name: event.Name, UpdatedAt: event.Time}
projection.Set(player.ID, &player)
}
// SetBirthdayPlayerEvent is a sample event that can be used for testing
type SetBirthdayPlayerEvent struct {
ID uuid.UUID
Time time.Time
Birthday time.Time
}
// Resolve will change the name on a PlayerEntity
func (event *SetBirthdayPlayerEvent) Resolve(projection IProjection) {
player := *projection.Get(event.ID)
player.Birthday = event.Birthday
player.UpdatedAt = event.Time
}
func main() {
vault := EventVault{}
event1 := AddPlayerEvent{ID: uuid.NewV4(), Time: time.Now(), Name: "Lisa"}
vault.Append(&event1)
birthday, _ := time.Parse("2006-01-02", "2017-03-04")
event2 := SetBirthdayPlayerEvent{ID: event1.ID, Time: time.Now(), Birthday: birthday}
vault.Append(&event2)
}
我得到的错误是
./main.go:47: event.Time undefined (type IEvent has no field or method Time)
./main.go:79: event.Time undefined (type IEvent has no field or method Time)
./main.go:122: invalid indirect of projection.Get(event.ID) (type IEntity)
结构类型可能丢失了,所以我需要另一种存储方法将它们放入映射和切片中,但是如何在golang中声明一个类似
type IEntity interface {
}
您可以定义在没有类型转换的情况下使用此接口可以完成的所有操作。因此,这里您没有为这个接口定义任何功能。如果你想要功能,你必须给它一个方法,比如
type IEntity interface {
Time() time.Time
}
任何想要与该接口一起使用的类型都必须实现这些功能,即
func (a AddPlayerEvent) Time() time.Time {
return a.Time
}
然后你可以使用这些方法中的任何一种
func (projection *Projection) Append(event IEvent) {
...
event.Time()
...
}
还有两个注意事项
- 在遍历映射时,可以使用k,v:=范围my_映射
- *projection.Get(event.ID)尝试遵从非指针类型
Time()Time.Time
添加到IEvent
界面。此外,您需要在结构中实现一个名为Time()Time.Time
的方法,并将给定的第47,79行更新为event.Time()
。是的,但我发现这有两个问题,一个是我必须为所有属性添加一个getter/setter,另一个是如果我添加一个名为TreeEntity的实体,其属性为Kind string,分支[]分支等它们将具有不同的getter/setter,因为它们具有不同的属性,因此会中断解析方法。:/我已经习惯了松散类型的语言:/所以答案是我必须对添加到结构中的所有属性使用getter/setter,这并不是我所希望的答案,但也许这是唯一的方法。如果有另一种方法将slice/map中的引用保存到结构的混合中而不需要getter/setter,那就太好了,即使不需要接口也是如此。我确实尝试过用接球手/二传手,但没有成功,但我想我需要多练习一些,以使我的头脑清醒过来。感谢您的输入。好的,为了清楚起见,您不必使用接口作为传递给结构中的方法/存储区的类型,但是如果要使用接口,接口最好具有您需要的所有方法。将player.birth更改为setter需要我添加SetBirthday(time.time)由于并非所有实体都必须具有属性属性,因此对于IEntity,它打破了具有称为IEntity的通用接口的完整点/看到你的观点了,但我想我缺乏如何在单个数组/切片/映射中存储/检索混合结构类型的知识,如果没有接口也会非常好。你不能在切片/映射中存储混合类型,你必须使用类型断言。