Going crazy on golang maps

209 views
Skip to first unread message

Chris Polderman

unread,
Sep 23, 2017, 10:37:28 AM9/23/17
to golang-nuts

Hello!

I am working on a home automation project in Go. 

One of the components is polling all the sensors in a HUE bridge (this is some LED lighting system with smart lamps and buttons).

I am wanting to make a map, keyed on int to "Sensor" objects. These objects are located in a sensors package (so: sensors.Sensor).

My noob feeling says to do it like this:

   var previousSensorInfo map[int]sensors.Sensor = make(map[int]sensors.Sensor)

but the compiler says:

src/github.com/cpo/events/bridges/hue/hue.go:91:40: sensors.Sensor undefined (type *sensors.Sensors has no field or method Sensor)

(this is the line indicated above)

This sensors.Sensor type is a simple struct:

type Sensor struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
State State `json:"state,omitempty"`
Config Config `json:"config,omitempty"`
Type string `json:"type,omitempty"`
ModelID string `json:"modelid,omitempty"`
SWVersion string `json:"swversion,omitempty"`
ManufacturerName string `json:"manufacturername,omitempty"`
UniqueID string `json:"uniqueid,omitempty"`
}

The full code reads:

func (hue *HueBridge) pollSensors(sensors *sensors.Sensors) {
var previousSensorInfo map[int]sensors.Sensor = make(map[int]sensors.Sensor)
for true {
logger.Printf("Polling sensors")
sensorInfo, err := sensors.GetAllSensors()
if err == nil {
if previousSensorInfo == nil {
// first call
logger.Printf("Got %d sensors", len(sensorInfo))
for _, sensor := range sensorInfo {
previousSensorInfo[sensor.ID] = &sensor
}
} else {
// diff the two
for _, sensor := range sensorInfo {
prevSensor, found := previousSensorInfo[sensor.ID]
if found {
if hue.diffSensor(prevSensor, sensor) {
logger.Printf(" Previous: %s", prevSensor)
logger.Printf(" Now : %s", sensor)
}
} else {
logger.Printf("New sensor: %d", sensor.ID)
}
previousSensorInfo[sensor.ID] = sensor
}
}
}
time.Sleep(1 * time.Second)
}
}

What is going on here?

Regards,

Chris

Jan Mercl

unread,
Sep 23, 2017, 10:51:26 AM9/23/17
to Chris Polderman, golang-nuts
On Sat, Sep 23, 2017 at 4:37 PM Chris Polderman <chris.p...@gmail.com> wrote:

> The full code reads:

The full code is not full code. It lacks eg. the import declarations. Please provide a link to the really full code at a specific commit for others to reproduce.

--

-j

Tamás Gulácsi

unread,
Sep 23, 2017, 11:28:46 AM9/23/17
to golang-nuts

   previousSensorInfo := make(map[int]*sensors.Sensor)

Jakob Borg

unread,
Sep 23, 2017, 12:32:25 PM9/23/17
to Chris Polderman, golang-nuts
On 23 Sep 2017, at 15:27, Chris Polderman <chris.p...@gmail.com> wrote:
>
> func (hue *HueBridge) pollSensors(sensors *sensors.Sensors) {
> var previousSensorInfo map[int]sensors.Sensor = make(map[int]sensors.Sensor)

In your function declaration, you're declaring a parameter "sensors" of the type *sensors.Sensors. This shadows the *package* also called "sensors" inside the function.

//jb

Chris Polderman

unread,
Sep 24, 2017, 3:38:00 PM9/24/17
to golang-nuts


YES We have a winner!

I really did not see that!

Regards,

Chris

Op zaterdag 23 september 2017 18:32:25 UTC+2 schreef Jakob Borg:
Reply all
Reply to author
Forward
0 new messages