search
HomeBackend DevelopmentGolangQuick Start: Use Go language functions to implement a simple library management system

Quick Start: Use Go language functions to implement a simple library management system

Jul 30, 2023 am 09:18 AM
SimpleQuick Start: go functionslibrary management system

Quick Start: Using Go language functions to implement a simple library management system

Introduction:
With the continuous development of the field of computer science, the needs of software applications are becoming more and more diverse. As a common management tool, the library management system has also become one of the necessary systems for many libraries, schools and enterprises. In this article, we will use Go language functions to implement a simple library management system. Through this example, readers can learn the basic usage of functions in Go language and how to build a practical program.

1. Design ideas:
Let’s first take a look at what functions the library management system needs to have:

  1. Add books: Add new book information to the system.
  2. Delete book: Delete the specified book from the system based on the book number or name.
  3. Search for books: Query the detailed information of books from the system based on the book number, name or author.
  4. Modify the book: Modify the relevant information of the book according to the book number.
  5. Display books: Display all books in the system according to a certain format.

The design ideas are as follows:

  1. Use structures to represent book information, including number, name, author, publisher, price, etc.
  2. Use slices to store book information in the system.
  3. Define each functional function to correspond to the above requirements.

2. Code examples:
The following is a code example of using Go language functions to implement a simple library management system:

package main

import (
    "fmt"
)

// 图书结构体
type Book struct {
    Id     int
    Name   string
    Author string
    Press  string
    Price  float64
}

// 图书列表
var bookList []Book

// 添加图书
func addBook() {
    var book Book
    fmt.Println("请输入图书的编号:")
    fmt.Scanln(&book.Id)
    fmt.Println("请输入图书的名称:")
    fmt.Scanln(&book.Name)
    fmt.Println("请输入图书的作者:")
    fmt.Scanln(&book.Author)
    fmt.Println("请输入图书的出版社:")
    fmt.Scanln(&book.Press)
    fmt.Println("请输入图书的价格:")
    fmt.Scanln(&book.Price)

    bookList = append(bookList, book)
}

// 删除图书
func deleteBook() {
    var input string
    fmt.Println("请输入要删除的图书的编号或名称:")
    fmt.Scanln(&input)

    for i, book := range bookList {
        if book.Name == input || fmt.Sprintf("%v", book.Id) == input {
            bookList = append(bookList[:i], bookList[i+1:]...)
            fmt.Println("删除成功!")
            return
        }
    }

    fmt.Println("未找到要删除的图书!")
}

// 查找图书
func findBook() {
    var input string
    fmt.Println("请输入要查找的图书的编号、名称或作者:")
    fmt.Scanln(&input)

    for _, book := range bookList {
        if book.Name == input || fmt.Sprintf("%v", book.Id) == input || book.Author == input {
            fmt.Printf("编号:%v
名称:%v
作者:%v
出版社:%v
价格:%v
", book.Id, book.Name, book.Author, book.Press, book.Price)
            return
        }
    }

    fmt.Println("未找到相关图书!")
}

// 修改图书
func modifyBook() {
    var input string
    fmt.Println("请输入要修改的图书的编号:")
    fmt.Scanln(&input)

    for i, book := range bookList {
        if fmt.Sprintf("%v", book.Id) == input {
            fmt.Println("请输入新的图书名称:")
            fmt.Scanln(&bookList[i].Name)
            fmt.Println("请输入新的图书作者:")
            fmt.Scanln(&bookList[i].Author)
            fmt.Println("请输入新的图书出版社:")
            fmt.Scanln(&bookList[i].Press)
            fmt.Println("请输入新的图书价格:")
            fmt.Scanln(&bookList[i].Price)
            fmt.Println("修改成功!")
            return
        }
    }

    fmt.Println("未找到要修改的图书!")
}

// 展示图书
func showBooks() {
    fmt.Println("图书列表:")
    for _, book := range bookList {
        fmt.Printf("编号:%v
名称:%v
作者:%v
出版社:%v
价格:%v

", book.Id, book.Name, book.Author, book.Press, book.Price)
    }
}

// 主函数
func main() {
    for {
        fmt.Println("欢迎使用图书管理系统,请输入相应的操作序号:")
        fmt.Println("1. 添加图书")
        fmt.Println("2. 删除图书")
        fmt.Println("3. 查找图书")
        fmt.Println("4. 修改图书")
        fmt.Println("5. 展示图书")
        fmt.Println("6. 退出系统")

        var choice int
        fmt.Scanln(&choice)

        switch choice {
        case 1:
            addBook()
        case 2:
            deleteBook()
        case 3:
            findBook()
        case 4:
            modifyBook()
        case 5:
            showBooks()
        case 6:
            fmt.Println("感谢使用图书管理系统,再见!")
            return
        default:
            fmt.Println("输入有误,请重新输入!")
        }
    }
}

3. Summary:
This article introduces the use Go language function implements a simple library management system. Through a simple example, readers can learn the basic usage and practical application of Go language functions. Of course, this is just a simple library management system. If you want to implement more complex functions, further expansion and optimization are needed. I hope this article can be of some help to readers in learning and using Go language functions.

The above is the detailed content of Quick Start: Use Go language functions to implement a simple library management system. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Golang vs. C  : Code Examples and Performance AnalysisGolang vs. C : Code Examples and Performance AnalysisApr 15, 2025 am 12:03 AM

Golang is suitable for rapid development and concurrent programming, while C is more suitable for projects that require extreme performance and underlying control. 1) Golang's concurrency model simplifies concurrency programming through goroutine and channel. 2) C's template programming provides generic code and performance optimization. 3) Golang's garbage collection is convenient but may affect performance. C's memory management is complex but the control is fine.

Golang's Impact: Speed, Efficiency, and SimplicityGolang's Impact: Speed, Efficiency, and SimplicityApr 14, 2025 am 12:11 AM

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

C   and Golang: When Performance is CrucialC and Golang: When Performance is CrucialApr 13, 2025 am 12:11 AM

C is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.

Golang in Action: Real-World Examples and ApplicationsGolang in Action: Real-World Examples and ApplicationsApr 12, 2025 am 12:11 AM

Golang excels in practical applications and is known for its simplicity, efficiency and concurrency. 1) Concurrent programming is implemented through Goroutines and Channels, 2) Flexible code is written using interfaces and polymorphisms, 3) Simplify network programming with net/http packages, 4) Build efficient concurrent crawlers, 5) Debugging and optimizing through tools and best practices.

Golang: The Go Programming Language ExplainedGolang: The Go Programming Language ExplainedApr 10, 2025 am 11:18 AM

The core features of Go include garbage collection, static linking and concurrency support. 1. The concurrency model of Go language realizes efficient concurrent programming through goroutine and channel. 2. Interfaces and polymorphisms are implemented through interface methods, so that different types can be processed in a unified manner. 3. The basic usage demonstrates the efficiency of function definition and call. 4. In advanced usage, slices provide powerful functions of dynamic resizing. 5. Common errors such as race conditions can be detected and resolved through getest-race. 6. Performance optimization Reuse objects through sync.Pool to reduce garbage collection pressure.

Golang's Purpose: Building Efficient and Scalable SystemsGolang's Purpose: Building Efficient and Scalable SystemsApr 09, 2025 pm 05:17 PM

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Why do the results of ORDER BY statements in SQL sorting sometimes seem random?Why do the results of ORDER BY statements in SQL sorting sometimes seem random?Apr 02, 2025 pm 05:24 PM

Confused about the sorting of SQL query results. In the process of learning SQL, you often encounter some confusing problems. Recently, the author is reading "MICK-SQL Basics"...

Is technology stack convergence just a process of technology stack selection?Is technology stack convergence just a process of technology stack selection?Apr 02, 2025 pm 05:21 PM

The relationship between technology stack convergence and technology selection In software development, the selection and management of technology stacks are a very critical issue. Recently, some readers have proposed...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor