package main import ( "fmt" "log" ) type States map[string]bool type Cache struct { Tx chan MQTTMessage States map[string]bool } func NewCache(tx chan MQTTMessage) Cache { var cache Cache cache.Tx = tx cache.States = make(States) return cache } func (c *Cache) Update(states States) { if c.States == nil { log.Fatal("Cache states is nil.") } if states == nil { log.Fatal("Given states is nil.") } for id, state := range states { cached, known := c.States[id] if !known || cached != state { topic := fmt.Sprintf("contact/%s/state", id) var payload []byte if state == true { payload = []byte("open") } else { payload = []byte("closed") } c.Tx <- MQTTMessage{ Topic: topic, Payload: payload, } } c.States[id] = state } }