Golang
Golang

 

Manage library books and members effectively with this simple Library Management System created in the Go programming language. This program allows for efficient management of books, members, and their interactions within a library system.

Introduction

The Library Management System is an essential tool for managing the collection of books, member records, and various library operations. It helps library administrators easily manage books, track available inventory, and handle membership registration. This system also ensures that users can borrow and return books with ease, making the entire process streamlined and efficient.

Objective

The objective of this program is to develop a library management system where the library administrator can add books, view book details, manage members, and track borrowed books. This system is written in Go programming language and is designed to be lightweight, fast, and scalable for small to medium-sized libraries.

Code Implementation


package main

import "fmt"

type Book struct {
    ID     int
    Title  string
    Author string
    Status string // "available" or "borrowed"
}

type Member struct {
    ID   int
    Name string
}

var books []Book
var members []Member

func addBook(id int, title, author string) {
    books = append(books, Book{ID: id, Title: title, Author: author, Status: "available"})
    fmt.Println("Book added successfully.")
}

func addMember(id int, name string) {
    members = append(members, Member{ID: id, Name: name})
    fmt.Println("Member added successfully.")
}

func borrowBook(bookID, memberID int) {
    for i, book := range books {
        if book.ID == bookID && book.Status == "available" {
            books[i].Status = "borrowed"
            fmt.Printf("Book '%s' borrowed by member '%s'.\n", book.Title, members[memberID-1].Name)
            return
        }
    }
    fmt.Println("Sorry, the book is not available for borrowing.")
}

func returnBook(bookID int) {
    for i, book := range books {
        if book.ID == bookID && book.Status == "borrowed" {
            books[i].Status = "available"
            fmt.Printf("Book '%s' returned successfully.\n", book.Title)
            return
        }
    }
    fmt.Println("This book was not borrowed.")
}

func showBooks() {
    fmt.Println("Books in Library:")
    for _, book := range books {
        fmt.Printf("ID: %d, Title: '%s', Author: '%s', Status: %s\n", book.ID, book.Title, book.Author, book.Status)
    }
}

func showMembers() {
    fmt.Println("Members in Library:")
    for _, member := range members {
        fmt.Printf("ID: %d, Name: '%s'\n", member.ID, member.Name)
    }
}

func main() {
    addBook(1, "Go Programming", "John Doe")
    addBook(2, "Learn Go", "Jane Smith")
    addMember(1, "Alice")
    addMember(2, "Bob")

    showBooks()
    showMembers()

    borrowBook(1, 1) // Alice borrows 'Go Programming'
    returnBook(1)    // Alice returns 'Go Programming'

    showBooks()
}
            

Explanation of the Program Structure

The program is organized into several key sections:

  • Data Structures: The program defines two key structures: Book and Member, which represent books in the library and library members, respectively. Each structure contains essential information related to books and members.
  • Functions: The program defines several functions for adding books, adding members, borrowing and returning books, and displaying available books and members.
  • Borrowing and Returning Books: The borrowBook() and returnBook() functions manage the borrowing and returning of books. They ensure that a book is available before borrowing and that it is marked as available when returned.
  • Main Function: In the main() function, books and members are added, and the system demonstrates borrowing and returning books.

How to Run the Program

  1. Install Go programming language from Go’s official website.
  2. Save the above code into a file named library.go.
  3. Open a terminal or command prompt and navigate to the directory where library.go is saved.
  4. Run the command go run library.go to execute the program.
  5. You will see the library management system’s output in the terminal, including the list of books, members, borrowing actions, and returned books.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)