Go Install
- go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
- go install github.com/pressly/goose/v3/cmd/goose@latest
- go get github.com/google/uuid
Migration
- create folder sql/schema
- create new file 001_users.sql
-- +goose Up CREATE TABLE users ( id UUID PRIMARY KEY, created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, name TEXT NOT NULL ); -- +goose Down DROP TABLE users;
- cd sql/schema
- goose postgres postgres://postgres:postgres@localhost:5432/go-restapi up
Generate Model using Sqlc
- create folder in sql/queries
- create sql file users.sql
-- name: CreateUser :one
INSERT INTO users (id, created_at, updated_at, name) VALUES ($1, $2, $3, $4)
RETURNING *;
- run command
sqlc generate
Running
- module init : go mod init github.com/tedyfd/go-restapi
- build to binary files : go build .
- running : go run .
- get module : go get github.com/joho/godotenv
- download module : go mod vendor