To start from the beginning, including initializing a Go module for your project, here's a step-by-step guide:
First, create your project directory and navigate to it:
mkdir my-go-project
cd my-go-project
Then, initialize a Go module:
go mod init github.com/your-username/my-go-project
This command creates a go.mod
file that tracks your dependencies and module versions. Replace github.com/your-username/my-go-project
with your actual project path.
Organize your project folder. You can follow this structure:
my-go-project/
├── db/
│ ├── migrations/ # SQL migration files go here
├── cmd/
│ └── main.go # Entry point for your app
├── go.mod # Go module file
└── go.sum # Dependencies and their versions
Install necessary libraries for routing, ORM, and database migrations:
# Install GORM and the required database driver (PostgreSQL here)
go get -u gorm.io/gorm
go get -u gorm.io/driver/postgres
# Install golang-migrate library for migrations
go get -u github.com/golang-migrate/migrate/v4
# Install Chi router (or use another router like Gorilla Mux)
go get -u github.com/go-chi/chi/v5
Before you start coding, you'll need to create migration files that contain the SQL queries to set up your database tables.
Install the golang-migrate CLI if you haven't already:
brew install golang-migrate
Then create a migration file for your schema:
migrate create -ext sql -dir db/migrations -seq create_users_table
This file contains the routing setup and GORM initialization.
package main
import (
"log"
"net/http"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/go-chi/chi/v5"
)
func main() {
// Set up the database connection
dsn := "host=localhost user=my_user password=my_password dbname=my_db port=5432 sslmode=disable"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal("failed to connect to database:", err)
}
// Auto-migrate your schema (optional)
db.AutoMigrate(&User{})
// Set up the router
r := chi.NewRouter()
// Define routes
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world!"))
})
log.Println("Starting server on :8080")
http.ListenAndServe(":8080", r)
}
// GORM model for the 'users' table
type User struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"size:255;not null"`
Email string `gorm:"size:255;unique;not null"`
}
Now that your schema is set up in code, you can generate SQL migration files.
-
Create the SQL migration files using golang-migrate (if not already done):
migrate create -ext sql -dir db/migrations -seq create_users_table
-
Fill the generated migration files:
-
db/migrations/000001_create_users_table.up.sql
CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) UNIQUE NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
-
db/migrations/000001_create_users_table.down.sql
DROP TABLE IF EXISTS users;
-
To apply the migrations and initialize your database schema:
migrate -database "postgres://user:password@localhost:5432/my_db?sslmode=disable" -path db/migrations up
To start your Go application:
go run cmd/main.go
This will start the server, auto-migrate the database (if you enabled auto-migration), and serve the API.
Visit http://localhost:8080
in your browser or use curl
to test:
curl http://localhost:8080
This should return a "Hello, world!" response.
With this setup, you now have a Go project with initialized migrations, database connections, and routing ready for backend API development.