I have a composite type defined for use in a table:
CREATE TYPE duration AS (
hours NUMERIC,
minutes NUMERIC
);
CREATE TABLE foo (
id SERIAL PRIMARY KEY,
my_duration duration
);
INSERT INTO foo (id, my_duration) VALUES
(1, ROW(1, 30));
How can I scan this with database/sql
? When I implement the scanner interface with
func (d *Duration) Scan(value interface{}) error {
log.Println(value)
if value == nil {
*d = Duration{
Hours: -1,
Minutes: -1,
}
return nil
}
if duration, ok := value.(Duration); ok {
*d = Duration(duration)
return nil
}
return errors.New("Failed to scan duration")
}
The type of the interface comes out as []uint8
, and the value itself is an encoded array, something like [40 50 44 51 51 41]
. How can I parse the composite type properly?
edit: I have a Duration
struct defined as
type Duration struct {
Hours int
Minutes int
}