Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

dgw

> 数据库
Open source

dgw generates Golang struct, and simple Table/Row Data Gateway functions from PostgreSQL table metadata

190 stars0 likes1 views
WebsiteGitHub

About

dgw generates Golang struct, and simple Table/Row Data Gateway functions from PostgreSQL table metadata

dgw

Description

dgw generates Golang struct, and simple Table/Row Data Gateway functions from PostgreSQL table metadata. Heavily inspired by xo.

Why created

Personally, I prefer Table/Row Data Gateway over ORM/Query Builder approach when using Go with RDBMS. However, it is very time consuming, tedious, and error-prone to write a lot of columns, query place holders, and struct fields that all have to be exactly in order even for a simple select/insert statement. dgw generate Go struct, and simple functions to get/create row from PostgreSQL table definitions to avoid manually writing simple but tedious SQL.

  • dgw can properly detect autogenerated column (e.g. serial, bigserial), and composit primary key to create appropriate SQL.
  • dgw has ability to easily customize PostgreSQL column type Go type mapping using toml config file.

Installation

bash
brew install kanmu/tools/dgw

How to use

…
dgw postgres://dbuser@localhost/dbname?sslmode=disable

Excluding columns

--exclude-column (-X) drops a column from the generated struct field, and from the generated INSERT/SELECT statements. It must be given in table.column format, so a bare column name is rejected. Pass the flag multiple times to exclude more than one column.

dgw postgres://dbuser@localhost/dbname?sslmode=disable -X t1.nullable_str -X t1.xml_data

dgw stops with an error, instead of generating broken code, when the exclusion is:

  • a primary key column (GetXByPk would no longer match the table)
  • a table or column which does not exist in the schema (usually a typo)
  • every column of a table (use --exclude to skip the whole table)

Example

  • https://github.com/kanmu/dgw/tree/master/example
…

Generate Go code by the following command.

$ dgw postgres://dgw_test@localhost/dgw_test?sslmode=disable --typemap=./typemap.toml --schema=public --package=dgwexample --output=example.go
…

Test

Using Docker (Recommended)

bash
# Start PostgreSQL 16 with pre-configured test database and user
$ docker compose up -d

# Wait for PostgreSQL to be ready
$ docker compose exec postgres pg_isready -U dgw_test

# Run tests
$ go test -v

# Stop PostgreSQL when done
$ docker compose down

Manual Setup

bash
$ psql -d template1
> CREATE USER dgw_test;
> CREATE DATABASE  dgw_test OWNER dgw_test;
> \q
$ go test -v

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Godbgeneratorgopostgresql

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库