Library Management System Project in Go (Golang)

In this post, we will dive into creating a Library Management System using Go. This system will allow users to manage books and check out or return them.

System Overview

Our Library Management System will handle: 

  1. Adding a new book. 
  2. Listing all books. 
  3. Borrowing a book. 
  4. Returning a book. 

Implementation

1. Define Data Structures:

The Book structure contains information about each book, and the Library contains a slice of all books.

package main

import "fmt"

type Book struct {
	ID     int
	Title  string
	Author string
	IsOut  bool
}

type Library struct {
	books []Book
}

2. Define Library Methods:

The Library structure has methods for adding books, listing books, borrowing, and returning books. We use slice operations and range loops to manage books efficiently.

func (l *Library) AddBook(title string, author string) {
	book := Book{
		ID:     len(l.books) + 1,
		Title:  title,
		Author: author,
		IsOut:  false,
	}
	l.books = append(l.books, book)
}

func (l *Library) ListBooks() {
	for _, book := range l.books {
		status := "Available"
		if book.IsOut {
			status = "Borrowed"
		}
		fmt.Printf("ID: %d, Title: %s, Author: %s, Status: %s\n", book.ID, book.Title, book.Author, status)
	}
}

func (l *Library) BorrowBook(id int) {
	for index, book := range l.books {
		if book.ID == id {
			l.books[index].IsOut = true
			break
		}
	}
}

func (l *Library) ReturnBook(id int) {
	for index, book := range l.books {
		if book.ID == id {
			l.books[index].IsOut = false
			break
		}
	}
}

3. Main Function:

The main() function demonstrates a simple workflow: adding books, listing them, borrowing a book, and then returning it.
func main() {
	lib := Library{}

	lib.AddBook("The Go Programming Language", "Alan A. A. Donovan")
	lib.AddBook("Go in Action", "William Kennedy")

	fmt.Println("List of Books:")
	lib.ListBooks()

	fmt.Println("\nBorrowing Book with ID 1")
	lib.BorrowBook(1)
	lib.ListBooks()

	fmt.Println("\nReturning Book with ID 1")
	lib.ReturnBook(1)
	lib.ListBooks()
}

Complete Code with Output

package main

import "fmt"

// Define the Book structure
type Book struct {
	ID     int
	Title  string
	Author string
	IsOut  bool
}

// Define the Library structure
type Library struct {
	books []Book
}

// Method to add a new book to the library
func (l *Library) AddBook(title string, author string) {
	book := Book{
		ID:     len(l.books) + 1,
		Title:  title,
		Author: author,
		IsOut:  false,
	}
	l.books = append(l.books, book)
}

// Method to list all books in the library
func (l *Library) ListBooks() {
	for _, book := range l.books {
		status := "Available"
		if book.IsOut {
			status = "Borrowed"
		}
		fmt.Printf("ID: %d, Title: %s, Author: %s, Status: %s\n", book.ID, book.Title, book.Author, status)
	}
}

// Method to borrow a book
func (l *Library) BorrowBook(id int) {
	for index, book := range l.books {
		if book.ID == id {
			l.books[index].IsOut = true
			break
		}
	}
}

// Method to return a book
func (l *Library) ReturnBook(id int) {
	for index, book := range l.books {
		if book.ID == id {
			l.books[index].IsOut = false
			break
		}
	}
}

// Main function
func main() {
	lib := Library{}

	// Add a couple of books
	lib.AddBook("The Go Programming Language", "Alan A. A. Donovan")
	lib.AddBook("Go in Action", "William Kennedy")

	// List all books
	fmt.Println("List of Books:")
	lib.ListBooks()

	// Borrow the first book
	fmt.Println("\nBorrowing Book with ID 1")
	lib.BorrowBook(1)
	lib.ListBooks()

	// Return the first book
	fmt.Println("\nReturning Book with ID 1")
	lib.ReturnBook(1)
	lib.ListBooks()
}

Output:

List of Books:
ID: 1, Title: The Go Programming Language, Author: Alan A. A. Donovan, Status: Available
ID: 2, Title: Go in Action, Author: William Kennedy, Status: Available

Borrowing Book with ID 1
ID: 1, Title: The Go Programming Language, Author: Alan A. A. Donovan, Status: Borrowed
ID: 2, Title: Go in Action, Author: William Kennedy, Status: Available

Returning Book with ID 1
ID: 1, Title: The Go Programming Language, Author: Alan A. A. Donovan, Status: Available
ID: 2, Title: Go in Action, Author: William Kennedy, Status: Available

Conclusion



The Go language offers a concise and efficient way to build systems like our Library Management System. While our system is basic, Go’s extensive standard library and features allow us to scale up, adding features like persistence, networking, or even integrating with a front end to create a web application.

The Go language combines the ease of dynamic languages with the power of statically-typed languages, making it an excellent choice for various applications, from web servers to system tools.

Comments