Building a Library Management System in Go
In this article, let's explore a Library Management System (LMS) implemented in Go, highlighting its core features, design decisions, and key code snippets.
Core Features of the Library Management System
- Book Management
The system supports multiple copies of each book, allowing for efficient tracking and management of inventory. Each book has properties such as ID, title, author, publication year, and a slice of BookItem, which represents the individual copies.
type Book struct {
ID int
BookItem []BookItem
Title string
Author string
PublishedYear string
mu sync.RWMutex
}
- Member Management
Members can borrow books, and the system tracks their borrowing history. Each member has a borrowing quota, ensuring they can borrow a limited number of books at any given time.
type Member struct {
ID int
Name string
ContactInfo string
CurrentBorrowed []*BookItem
BorrowHistory []*BookItem
}
- Borrowing and Returning Books
The borrowing mechanism checks for available copies and updates their status accordingly. The system allows members to return books, updating the book's status and tracking the transaction in the member's borrowing history.
func (m *Member) AddBorrowedBook(bookItem *BookItem) {
m.CurrentBorrowed = append(m.CurrentBorrowed, bookItem)
}
func (l *Library) BorrowBookByMember(memberID int, bookID int) *BookItem {
// Logic to borrow a book
}
- Concurrency Control
Utilizing Go's concurrency features, the system handles multiple borrowing and returning requests simultaneously. The use of sync.RWMutex ensures that book availability checks and updates are thread-safe, preventing race conditions.
func (b *Book) IsBookAvailable() bool {
b.mu.RLock()
defer b.mu.RUnlock()
for _, bookCopy := range b.BookItem {
if bookCopy.Status == Available {
return true
}
}
return false
}
- Overdue Book Management
The system checks if borrowed books are overdue, implementing business rules to notify members and possibly charge fines.
func (bi *BookItem) IsOverdue() bool {
if bi.Status != Borrowed {
return false
}
return time.Since(bi.LastBorrowed) > time.Hour*24*7
}
Design Decisions
Why Go?
Go was chosen for its simplicity, efficiency, and built-in support for concurrency, which is crucial for handling multiple requests in a library setting. Its strong typing and compile-time checks help reduce bugs and improve code maintainability.
Singleton Pattern for Library Instance
The system uses a singleton pattern to manage a single instance of the library. This design ensures that all operations (adding books, managing members) are centralized, simplifying resource management.
var (
libraryInstance *Library
once sync.Once
)
func GetLibraryInstance() *Library {
once.Do(func() {
libraryInstance = &Library{books: make(map[int]*Book), members: make(map[int]*Member)}
})
return libraryInstance
}
Encapsulation and Data Protection
The use of mutexes (sync.RWMutex) protects shared resources and ensures that concurrent access does not lead to inconsistent states. This encapsulation is crucial in a multi-user environment where multiple members may be interacting with the system simultaneously.
Please explore the complete code and contribute to further enhancements in the following repository:
thesaltree / low-level-design-golang
Low level system design problems solutions in Golang
Low-Level System Design in Go
Welcome to the Low-Level System Design in Go repository! This repository contains various low-level system design problems and their solutions implemented in Go. The primary aim is to demonstrate the design and architecture of systems through practical examples.
Table of Contents
Overview
Low-level system design involves understanding the core concepts of system architecture and designing scalable, maintainable, and efficient systems. This repository will try to cover solutions of various problems and scenarios using Go.
Parking Lot System
The first project in this repository is a Parking Lot System. This system simulates a parking lot where vehicles can be parked and unparked. It demonstrates:
- Singleton design pattern for managing the parking lot instance.
- Handling different types of vehicles (e.g., cars, trucks).
- Parking space management across multiple floors.
- Payment processing for parked vehicles.
Top comments (0)