A simple web-based phonebook application built with Go to demonstrate fundamental Go web development concepts including HTTP handling, database operations, authentication, and templating.
This project is designed to help developers learn:
- Go web server basics with
net/http - SQLite database integration
- HTML templating
- User authentication with bcrypt
- MVC architecture pattern in Go
- Middleware implementation
- Environment configuration
- Logging with structured logging
phonebook/
├── main.go # Application entry point and server setup
├── go.mod # Go modules file
├── go.sum # Go modules checksums
├── phonebook.db # SQLite database file
├── db/
│ ├── db.go # Database connection logic
│ └── schema.go # Database schema creation
├── models/
│ ├── contact.go # Contact model and database operations
│ └── user.go # User model and authentication logic
├── handlers/
│ ├── auth.go # Authentication handlers
│ ├── contact.go # Contact CRUD handlers
│ ├── user.go # User management handlers
│ ├── general.go # General utility handlers
│ └── templates.go # Template rendering utilities
├── templates/ # HTML templates
│ ├── layout.html # Base layout template
│ ├── contacts.html # Contact listing page
│ ├── login.html # Login form
│ └── ... # Other template files
└── static/
└── css/
└── style.css # Application styles
- Contact Management: Create, read, update, and delete contacts
- User Management: User registration and profile management
- Authentication: Login/logout with password hashing
- Search: Search contacts by name or phone number
- Responsive UI: Clean HTML interface with CSS styling
- Soft Delete: Records are marked as inactive instead of being permanently deleted
- Audit Trail: Track who created/updated records and when
- Session Management: Cookie-based authentication
- Logging: Structured logging with different levels
- Environment Configuration: Development/production environment support
- Go 1.24.4 or later
- SQLite (handled by the modernc.org/sqlite driver)
-
Clone the repository
git clone https://github.com/coolwolf/GoPhonebookSample.git cd phonebook -
Install dependencies
go mod tidy
-
Create environment file Create a
.envfile in the root directory:APP_ENV=development
-
Run the application
go run main.go
or
go run . -
Access the application Open your browser and navigate to
http://localhost:8080
mux := http.NewServeMux()
mux.HandleFunc("/", handlers.ListContactsHandler)
http.ListenAndServe(":8080", loggingMiddleware(mux))- Using SQLite with prepared statements
- Connection pooling with
sql.DB - CRUD operations with proper error handling
handlers.Tmpl.ExecuteTemplate(w, "main-layout", data)func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Logging logic
next.ServeHTTP(w, r)
})
}- Using bcrypt for password hashing
- Secure password storage and verification
- Cookie-based authentication
- User session handling
- modernc.org/sqlite: Pure Go SQLite driver
- golang.org/x/crypto: For bcrypt password hashing
- github.com/sirupsen/logrus: Structured logging
- github.com/joho/godotenv: Environment variable loading
The application uses environment variables for configuration:
APP_ENV: Set to "development" for debug logging, "production" for warn level
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
in_use INTEGER DEFAULT 1,
inserted_at DATETIME DEFAULT CURRENT_TIMESTAMP,
inserted_by INTEGER,
updated_at DATETIME,
updated_by INTEGER
);CREATE TABLE contacts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
phone TEXT NOT NULL,
in_use INTEGER DEFAULT 1,
inserted_at DATETIME DEFAULT CURRENT_TIMESTAMP,
inserted_by INTEGER,
updated_at DATETIME,
updated_by INTEGER
);| Method | Route | Description |
|---|---|---|
| GET | / |
List all contacts (home page) |
| GET | /contacts |
List all contacts |
| GET | /contacts/new |
Show new contact form |
| POST | /contacts/create |
Create new contact |
| GET | /contacts/edit |
Show edit contact form |
| POST | /contacts/update |
Update existing contact |
| POST | /contacts/delete |
Delete contact (soft delete) |
| GET | /users |
List all users |
| GET | /users/new |
Show new user form |
| POST | /users/create |
Create new user |
| GET | /users/edit |
Show edit user form |
| POST | /users/update |
Update existing user |
| POST | /users/delete |
Delete user (soft delete) |
| GET | /login |
Show login form |
| POST | /dologin |
Process login |
| GET | /logout |
Logout user |
- Start the server:
go run main.go - The application will create the SQLite database and tables automatically
- Access the web interface at
http://localhost:8080 - Create a user account to start managing contacts
To extend your Go knowledge, consider adding:
- Testing: Write unit tests and integration tests
- API Endpoints: Add JSON API endpoints alongside HTML interface
- Validation: Implement input validation and error handling
- Pagination: Add pagination for large contact lists
- Docker: Containerize the application
- Configuration: Advanced configuration management
- Graceful Shutdown: Implement proper server shutdown handling
This is a learning project! Feel free to:
- Fork the repository
- Add new features
- Improve the code structure
- Add tests
- Update documentation
This project is created for educational purposes. Feel free to use it for learning and teaching Go web development.
Happy Learning! 🚀
This phonebook application demonstrates core Go web development patterns in a simple, understandable way. Each component is designed to showcase different aspects of Go programming while building a functional web application.