PostgreSQL arrays are materialized as Go slices. Currently, only one-dimensional arrays are supported.
CREATE TABLE places (
name text not null,
tags text[]
);
package db
type Place struct {
Name string
Tags []string
}
All PostgreSQL time and date types are returned as time.Time
structs. For
null time or date values, the NullTime
type from database/sql
is used.
CREATE TABLE authors (
id SERIAL PRIMARY KEY,
created_at timestamp NOT NULL DEFAULT NOW(),
updated_at timestamp
);
package db
import (
"database/sql"
"time"
)
type Author struct {
ID int
CreatedAt time.Time
UpdatedAt sql.NullTime
}
PostgreSQL enums are mapped to an aliased string type.
CREATE TYPE status AS ENUM (
'open',
'closed'
);
CREATE TABLE stores (
name text PRIMARY KEY,
status status NOT NULL
);
package db
type Status string
const (
StatusOpen Status = "open"
StatusClosed Status = "closed"
)
type Store struct {
Name string
Status Status
}
For structs, null values are represented using the appropriate type from the
database/sql
package.
CREATE TABLE authors (
id SERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);
package db
import (
"database/sql"
)
type Author struct {
ID int
Name string
Bio sql.NullString
}
The Go standard library does not come with a uuid
package. For UUID support,
sqlc uses the excellent github.com/google/uuid
package.
CREATE TABLE records (
id uuid PRIMARY KEY
);
package db
import (
"github.com/google/uuid"
)
type Author struct {
ID uuid.UUID
}