From 857446f1f6c1e356b399af9a21f4bf5aaa8feb1f Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Thu, 9 Feb 2023 08:22:51 +0100 Subject: [PATCH] Make Open return a nil interface on failure Closes #154 Signed-off-by: Daniel Lublin --- serial.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/serial.go b/serial.go index 2bb7f3e..6b4869c 100644 --- a/serial.go +++ b/serial.go @@ -77,7 +77,13 @@ type ModemOutputBits struct { // Open opens the serial port using the specified modes func Open(portName string, mode *Mode) (Port, error) { - return nativeOpen(portName, mode) + port, err := nativeOpen(portName, mode) + if err != nil { + // Return a nil interface, for which var==nil is true (instead of + // a nil pointer to a struct that satisfies the interface). + return nil, err + } + return port, err } // GetPortsList retrieve the list of available serial ports