feat: Add method for opening devices
This commit is contained in:
parent
945b295bdf
commit
6c8e8c3003
1 changed files with 23 additions and 0 deletions
23
device.go
Normal file
23
device.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
var devices map[string]io.ReadWriteCloser
|
||||
|
||||
func GetDevice(name string) (io.ReadWriteCloser, error) {
|
||||
if dev, ok := devices[name]; ok {
|
||||
return dev, nil
|
||||
}
|
||||
|
||||
dev, err := os.OpenFile("/dev/" + name, os.O_RDWR, 0644)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open: %w", err)
|
||||
}
|
||||
|
||||
devices[name] = dev
|
||||
return dev, nil
|
||||
}
|
Loading…
Reference in a new issue