brian-armstrong / gpio

Go library to do GPIO on systems with /sys/class/gpio (sysfs)
BSD 3-Clause "New" or "Revised" License
140 stars 50 forks source link

openPin() architect improvement #12

Open ixvick opened 6 years ago

ixvick commented 6 years ago

in func openPin(p Pin, write bool) Pin { }

It would be better not to call fmt.Printf() and os.Exit(1) inside openPin() instead please return p, err

ixvick commented 6 years ago
func openPin(p Pin, write bool) (Pin, error) {
    flags := os.O_RDONLY
    if write {
        flags = os.O_RDWR
    }
    f, err := os.OpenFile(fmt.Sprintf("/sys/class/gpio/gpio%d/value", p.Number), flags, 0600)
    if err != nil {
                return p, err
    }
    p.f = f
    return p, nil
}