Project Overview & Structure
Part of the Object Oriented Programming section of Coddy's GO journey — lesson 98 of 107.
Challenge
EasyLet's kick off the Library Management System project by setting up the foundational structure! In this first step, you'll create the basic scaffolding that will support all the features we'll build in upcoming lessons.
You'll organize your code across three files to establish clean separation of concerns:
book.go: Define aBookstruct that represents a library book. Each book should have anID,Title,Author, andAvailablestatus (boolean). Create a constructor functionNewBookthat takes the ID, title, and author as parameters and returns a pointer to a new Book withAvailableset totrueby default. Also add aString()method that returns the book's information in the format:[ID] Title by Authoruser.go: Define aUserstruct representing a library member withID,Name, andEmailfields. Create a constructor functionNewUserthat takes all three values and returns a pointer to a new User. Add aString()method that returns:User: Name (Email)main.go: Bring everything together by reading input to create books and users, then display their information.Read the number of books to create. For each book, read its ID, title, and author on separate lines. Then read the number of users, and for each user read their ID, name, and email on separate lines.
After creating all items, print each book's string representation on its own line, followed by each user's string representation on its own line.
The following inputs will be provided:
- Number of books
- For each book: ID, title, and author (each on separate lines)
- Number of users
- For each user: ID, name, and email (each on separate lines)
For example, given:
2
B001
The Go Programming Language
Alan Donovan
B002
Clean Code
Robert Martin
1
U001
Alice Smith
alice@library.comYour output should be:
[B001] The Go Programming Language by Alan Donovan
[B002] Clean Code by Robert Martin
User: Alice Smith (alice@library.com)And given:
1
B100
Design Patterns
Gang of Four
2
U001
Bob Jones
bob@email.com
U002
Carol White
carol@email.comYour output should be:
[B100] Design Patterns by Gang of Four
User: Bob Jones (bob@email.com)
User: Carol White (carol@email.com)This foundation establishes the core data models you'll expand throughout the project. In the next lesson, you'll enhance these structs and add the borrowing functionality!
Try it yourself
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
// Read number of books
numBooksStr, _ := reader.ReadString('\n')
numBooks, _ := strconv.Atoi(strings.TrimSpace(numBooksStr))
// TODO: Create a slice to store books
// TODO: Read each book's information (ID, title, author on separate lines)
// and create Book objects using NewBook
for i := 0; i < numBooks; i++ {
id, _ := reader.ReadString('\n')
id = strings.TrimSpace(id)
title, _ := reader.ReadString('\n')
title = strings.TrimSpace(title)
author, _ := reader.ReadString('\n')
author = strings.TrimSpace(author)
// TODO: Create a new book and add it to your slice
}
// Read number of users
numUsersStr, _ := reader.ReadString('\n')
numUsers, _ := strconv.Atoi(strings.TrimSpace(numUsersStr))
// TODO: Create a slice to store users
// TODO: Read each user's information (ID, name, email on separate lines)
// and create User objects using NewUser
for i := 0; i < numUsers; i++ {
id, _ := reader.ReadString('\n')
id = strings.TrimSpace(id)
name, _ := reader.ReadString('\n')
name = strings.TrimSpace(name)
email, _ := reader.ReadString('\n')
email = strings.TrimSpace(email)
// TODO: Create a new user and add it to your slice
}
// TODO: Print each book's string representation
// TODO: Print each user's string representation
fmt.Println("TODO: Print output here")
}
All lessons in Object Oriented Programming
1Fundamentals of Go OOP
External FilesGo Workspace & ModulesPackages & ImportsExported vs Unexported NamesIntroduction to OOP in GoStructs as ClassesDefining Methods on StructsPointer vs Value ReceiversStruct InitializationConstructor FunctionsRecap - Simple Calculator4Interfaces
Introduction to InterfacesImplicit ImplementationInterface as ContractEmpty Interface (any)Type AssertionType SwitchInterface CompositionStringer & Error InterfacesRecap - Shape Calculator7Encapsulation
Exported vs Unexported FieldsPackage-Level EncapsulationGetter & Setter MethodsInformation Hiding in GoRecap - Student Records10Generics (Go 1.18+)
Introduction to GenericsType ParametersType ConstraintsGeneric StructsGeneric Methods WorkaroundRecap - Generic Collection2Types & Structs Deep Dive
Basic & Composite TypesCustom Type DefinitionsStruct TagsAnonymous StructsNested StructsZero Values & DefaultsRecap - Contact Book5Composition Over Inheritance
Why Go Has No InheritanceStruct Embedding BasicsMethod PromotionEmbedding Multiple StructsEmbedding vs AggregationShadowing Embedded MethodsRecap - Employee Hierarchy8Error Handling & OOP
The error InterfaceCustom Error TypesError Wrapping (fmt.Errorf)Sentinel Errorserrors.Is() and errors.As()Panic, Defer, and RecoverRecap - File Parser3Pointers & Memory
Pointer Basics in GoPointers to StructsPass by Value vs ReferenceThe new() FunctionGarbage Collection in GoRecap - Linked List Builder6Polymorphism in Go
Polymorphism via InterfacesDuck Typing in GoInterface Satisfaction RulesPolymorphic CollectionsDependency InjectionRecap - Payment Processor9Concurrency & OOP
Goroutines BasicsChannels & CommunicationBuffered vs Unbuffered ChanSelect Statementsync.Mutex & sync.RWMutexsync.WaitGroupThread-Safe Struct DesignRecap - Worker Pool12Advanced OOP Concepts
Functional Options PatternBuilder Pattern in GoMethod ChainingType Aliases vs DefinitionsReflection BasicsCode Generation Overview15Project: Library Management
Project Overview & StructureBook and User Structs