A simple web application for creating and viewing snippets, built with Go — following the structure and concepts from the book Let’s Go.
- Built with the Go programming language
- Create and view text snippets via HTTP
- MySQL database for persistent storage
- Clean separation of concerns (handlers, models, templates)
- Organized project structure suitable for testing and scaling
- Docker support for database containerization
.
├── cmd/web # Main web application entry point
│ ├── main.go # Application startup logic
│ ├── handlers.go # HTTP handlers
│ └── routes.go # Route definitions
├── internal/models # Database models
│ └── snippets.go # SnippetModel with DB methods
├── ui # HTML templates
├── docker # Docker-related files
│ └── mysql/init.sql # MySQL init script
├── go.mod # Go module definition
└── README.md # Project documentation
This project uses Docker to spin up a MySQL instance.
docker-compose up -dMySQL will be accessible at
localhost:3306with the credentials set indocker-compose.yml.
Make sure your DSN is correctly configured in main.go or passed via flags:
myuser:mypassword@tcp(127.0.0.1:3306)/snippetboxDB?parseTime=trueThen run:
go run ./cmd/webThe server will start on http://localhost:4000
curl.exe -i -L -X POST -d " " http://localhost:4000/snippet/createYou should receive a redirect to /snippet/view/{id} if everything is working.
- SQL queries are handled in the
SnippetModelmethods. - Routes are wired in
routes.goand tied to handler methods on theapplicationstruct. - The
internaldirectory is used for non-application-specific code and packages.
- Go 1.21+
- Docker (for MySQL setup)
- MySQL 8.0+ or compatible
- Add form-based snippet creation
- Render snippets with HTML templates
- Add validation
- Add sessions and authentication
This project is for learning purposes, inspired by the Let’s Go book by Alex Edwards.