package main import ( "container/ring" "fmt" "time" ) type Msg struct { Time time.Time Nick string Text string } type Last struct { *ring.Ring } func NewLast(n int) *Last { return &Last{ring.New(n)} } func (v *Last) Push(t time.Time, nick, text string) { v.Value = Msg{Time: t, Nick: nick, Text: text} v.Ring = v.Next() } // walk through buffer and find last message func (v *Last) Last(nick string) string { var msg string v.Do(func(v interface{}) { if l, ok := v.(Msg); ok { if l.Nick == nick { msg = l.Text } } }) return msg } func (v *Last) Dump(c chan string) { v.Do(func(v interface{}) { if l, ok := v.(Msg); ok { c <- fmt.Sprintf("%v <%v> %v", l.Time.UTC().Format(time.Kitchen), l.Nick, l.Text) } }) }