Skip to the content.

Struct Tags

goqu reads two struct tags when mapping structs to and from columns.

type User struct {
	ID        int64   `db:"id" goqu:"skipinsert,skipupdate"`
	FirstName string  `db:"first_name"`
	Address   *string `db:"address" goqu:"omitnil"`
	Notes     string  `db:"notes" goqu:"defaultifempty"`
}

goqu options

Option Applies to Effect
skipinsert INSERT The field is not included in INSERT statements. See Inserting.
skipupdate UPDATE The field is not included in UPDATE statements. See Updating.
skipselect SELECT The field is not added to the columns selected for the struct (Select(&s) and ScanStruct/ScanStructs without an explicit Select). The column is still scanned when it is selected, e.g. a column that comes from a join.
defaultifempty INSERT/UPDATE When the field is the zero value, DEFAULT is used instead of the value (sqlite3 does not support DEFAULT in UPDATE ... SET). See Inserting and Updating.
omitnil INSERT/UPDATE The field is skipped when it is a nil pointer. Useful for partial updates.
omitempty INSERT/UPDATE The field is skipped when it is the zero value (including nil pointers).
opaque all The field is one column and is never flattened into the columns of its struct type. See Opaque types.

Options can be combined, e.g. goqu:"skipinsert,skipupdate".

skipinsert, skipupdate and skipselect on a nested struct field apply to every column flattened out of that struct (e.g. Bar Bar goqu:”skipinsert”`` skips "bar"."b" on insert).

Opaque types

A struct-typed field is flattened into one column per field (Address Address becomes "address"."street", …) unless its type implements sql.Scanner. A type that is meant to be a single value but is not a sql.Scanner (a driver specific type such as a pgx type that only implements pgtype.BinaryEncoder/BinaryDecoder, or a struct that is stored as JSON) can be marked as opaque, so it is one column. The tag also works on a slice field (e.g. []string for a JSON column) to send it as one value instead of a list.

For one field, use the opaque option (it also works on an embedded field of an exported type):

type Event struct {
	ID      int
	Payload Payload `db:"payload" goqu:"opaque"`
}

For a type used in many structs, register it once at start up. Pointers to the type are covered too:

goqu.RegisterOpaque[Payload]()
// or, with a reflect.Type:
goqu.RegisterOpaqueType(reflect.TypeOf(Payload{}))

How the value is sent:

Notes:

Notes