Home > Article > Backend Development > Quick Start: Use Go language functions to implement a simple library 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:
The design ideas are as follows:
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!