Configuration to object, reflection or abstract factory?

(Luca Cavalllin) #1

I have a tiny IoT tool that given some configuration with sensor name and pin will read from these.
Let’s say I have:

        pin: 3

I want to parse this configuration so a new temperature sensor is created and listening to pin 3.
What is the best way to do so? Reflection? Abstract factories? I want to avoid a long list of switch which I find not very elegant.


(Johan Dahl) #2

It looks like yaml ( and if so could you use the packade to decode it like this

package main

import (


type T struct {
	Sensors struct {
		Temperature struct {
			Pin int

func main() {
	var data = `sensors:
        pin: 3`

	t := T{}

	err := yaml.Unmarshal([]byte(data), &t)
	if err != nil {
	fmt.Printf("t:%#v\n", t)
	fmt.Printf("pin: %d\n", t.Sensors.Temperature.Pin)

(system) #3

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.