This project demonstrates all core Go concepts through a practical task management application.
- Standard Go project layout (
cmd/,internal/,pkg/) - Module system (
go.mod) - Package organization
- โ Variables, constants, and types
- โ Functions and methods
- โ Structs and custom types
- โ Pointers
- โ Interfaces
- โ Error handling
- โ Slices and maps
- โ Control structures (if, for, switch)
- โ JSON marshaling/unmarshaling
- โ File I/O operations
- โ Concurrency (goroutines and channels)
- โ Mutex for thread safety
- โ HTTP server and routing
- โ Command-line flags
- โ Package organization
- โ Error handling patterns
- โ
Testing (
_test.gofiles) - โ Code structure and separation of concerns
golang-practice/
โโโ cmd/
โ โโโ taskmanager/
โ โโโ main.go # Application entry point
โโโ internal/
โ โโโ config/
โ โ โโโ config.go # Configuration management
โ โโโ handlers/
โ โ โโโ task_handler.go # HTTP request handlers
โ โโโ models/
โ โ โโโ task.go # Task data model
โ โโโ services/
โ โโโ task_service.go # Business logic
โ โโโ task_service_test.go # Unit tests
โโโ pkg/
โ โโโ utils/
โ โโโ demo.go # Utility functions and demos
โโโ go.mod # Go module file
โโโ GOLANG_PROJECT_STRUCTURE.md # Structure guide
โโโ README.md # This file
- Go 1.21 or higher installed
- Basic terminal knowledge
-
Navigate to the project directory:
cd /home/sucth/Programming/Projects/Golang-Practice -
Initialize Go module (if not already done):
go mod init golang-practice
-
Download dependencies:
go mod tidy
Shows demonstrations of all Go concepts:
CGO_ENABLED=0 go run cmd/taskmanager/main.go -mode=demoInteractive command-line interface:
CGO_ENABLED=0 go run cmd/taskmanager/main.go -mode=cliStarts a REST API server:
CGO_ENABLED=0 go run cmd/taskmanager/main.go -mode=server -port=8080Note: If you get a CGO error, use CGO_ENABLED=0 before the command. See TROUBLESHOOTING.md for details.
Then test the API:
# Get all tasks
curl http://localhost:8080/tasks
# Create a task
curl -X POST http://localhost:8080/tasks \
-H "Content-Type: application/json" \
-d '{"title":"New Task","description":"Task description","priority":"high"}'
# Get a specific task
curl http://localhost:8080/tasks/task-1234567890
# Health check
curl http://localhost:8080/healthRead GOLANG_PROJECT_STRUCTURE.md to understand Go project organization.
Look at internal/models/task.go:
- Struct definition
- Methods on structs
- Stringer interface
Check internal/services/task_service.go:
- Business logic
- Mutex for concurrency safety
- File I/O operations
- JSON operations
See internal/handlers/task_handler.go:
- HTTP server setup
- Request routing
- JSON encoding/decoding
- Error handling
Review pkg/utils/demo.go:
- Interfaces
- Error handling
- Goroutines and channels
- Pointers
- Collections (slices, maps)
go test ./internal/services/...- Each directory is a package
package mainis the entry point- Import paths use module name
type Task struct {
ID string
Title string
}
func (t *Task) Complete() {
// Method on Task
}type Shape interface {
Area() float64
}result, err := someFunction()
if err != nil {
return err
}go worker() // Goroutine
ch := make(chan string) // Channeljson.Marshal(data) // Convert to JSON
json.Unmarshal(data, &result) // Parse JSONRun all tests:
go test ./...Run with coverage:
go test -cover ./...After understanding this project, you can:
-
Add Features:
- User authentication
- Task categories/tags
- Task due dates
- Task search and filtering
-
Improve:
- Add database (PostgreSQL, MySQL)
- Add middleware (logging, authentication)
- Add WebSocket support
- Add Docker containerization
-
Build New Projects:
- REST API with database
- CLI tool
- Web scraper
- Microservice
Build executable:
go build -o taskmanager cmd/taskmanager/main.goRun the executable:
./taskmanager -mode=server- Read the code - Each file demonstrates specific concepts
- Run the demo mode - See all concepts in action
- Modify and experiment - Change code to see what happens
- Write tests - Practice test-driven development
- Build something - Apply what you learned
Happy coding! ๐