I have a custom type:
type CustomData struct{
data string
}
and I want to pass it as a string parameter to an SQL query
d := CustomData{data: "help, i need somebody"}
_, err := tx.Exec(ctx, `INSERT INTO tab (data) VALUES ($1)`, d)
I get an error
failed to encode args[0]: unable to encode CustomData{data: "help, i need somebody"} into text format for varchar (OID 1043): cannot find encode plan
Which interface should CustomData
implement to be unmarshalled as a string?
UPDATE
I implemented pgtype.TextValuer
interface as @Adrian wrote in the comment.
func (d *CustomData) TextValue() (pgtype.Text, error) {
return pgtype.Text{
String: d.data,
Valid: true,
}, nil
}
Unfortunately it didn't change an error message.
Per the package docs:
Per that package's documentation:
Meaning you can implement pgtype.TextScanner and pgtype.TextValuer on any type to create the behavior of a string value.