重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Go 语言入门练手项目系列
创新互联长期为千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为江汉企业提供专业的成都网站设计、做网站,江汉网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
这是一个基于go应用程序命令行的bookstore小项目,具体实现了图书的增删改查,适合刚入门go的朋友熟悉go的语法。
$ .\bookstore.exe get -all
Id Title Author
1 Go语言圣经 Ailen A
2 Java编程思想 Bruce Eckel
3 流畅的Python Luciano Ramalho
4 PHP权威指南 CENJW
$ .\bookstore.exe get -id 1
Id Title Author
1 Go语言圣经 Ailen A
$ .\bookstore.exe add -id 123 -title 'Head First Go' -author unknown
add successfully!
$ .\bookstore.exe add -id 1 -title test -author test
2022/06/24 18:05:44 Book already exists.
$ .\bookstore.exe update -id 123 -title 'head first go' -author UNKNOWN
update successfully!
$ .\bookstore.exe delete -id 123
Delete successfully!
本文来自创新互联建设站,作者:小谭,转载请注明原文链接:https://www.cdcxhl.com/article39/dsoicph.html
main.go
func main() {
// get book/books
getCmd := flag.NewFlagSet("get", flag.ExitOnError)
getAll := getCmd.Bool("all", false, "List all books")
getId := getCmd.String("id", "", "Get a book by id")
// add/update book
addCmd := flag.NewFlagSet("add", flag.ExitOnError)
addId := addCmd.String("id", "", "Book id")
addTitle := addCmd.String("title", "", "Book title")
addAuthor := addCmd.String("author", "", "Book author")
// delete book
deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
deleteId := deleteCmd.String("id", "", "Book id")
// check command
if len(os.Args) < 2 { log.Fatal("Expected get, add, update, delete commands.") } switch os.Args[1] { case "get": handleGetBooks(getCmd, getAll, getId) case "add": handleAddOrUpdateBook(addCmd, addId, addTitle, addAuthor, true) case "update": handleAddOrUpdateBook(addCmd, addId, addTitle, addAuthor, false) case "delete": handleDeleteBook(deleteCmd, deleteId) default: fmt.Println("Pls provide get, add, update, delete commands.") } }
books.go
type Book struct {
Id string `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
}
// get all the books logic
func handleGetBooks(getCmd *flag.FlagSet, all *bool, id *string) {
getCmd.Parse(os.Args[2:])
if !*all && *id == "" {
fmt.Println("subcommand --all or --id needed") // PrintDefault打印集合中所有注册好的flag的默认值。除非另外配置,默认输出到标准错误输出中。
getCmd.PrintDefaults()
os.Exit(1)
}
if *all {
books := getBooks()
fmt.Println("Id\tTitle\t\t\tAuthor\t")
for _, book := range books {
fmt.Printf("%v\t%v\t\t%v\t\n", book.Id, book.Title, book.Author)
}
}
if *id != "" {
var isExist bool
books := getBooks()
fmt.Println("Id\tTitle\t\tAuthor\t")
for _, book := range books {
if book.Id == *id {
fmt.Printf("%v\t%v\t%v\t\n", book.Id, book.Title, book.Author)
isExist = true
}
}
if !isExist {
log.Fatal("Book not found!")
}
}
}
// add or update a book logic
// addBook: true denote add a new book to file, false denote update a book
func handleAddOrUpdateBook(addCmd *flag.FlagSet, id, title, author *string, addBook bool) {
addCmd.Parse(os.Args[2:])
if *id == "" || *title == "" || *author == "" {
addCmd.PrintDefaults()
log.Fatal("Pls provide id, title, author commands.")
}
books := getBooks()
var newBook Book
if addBook {
for _, book := range books {
if book.Id == *id {
log.Fatal("Book already exists.")
} else {
newBook = Book{*id, *title, *author}
books = append(books, newBook)
break
}
}
} else {
var isExist bool
for i, book := range books {
if book.Id == *id {
books[i] = Book{Id: *id, Title: *title, Author: *author}
isExist = true
}
}
err := saveBooks(books)
checkError(err)
if !isExist {
log.Fatal("Book not found")
}
}
saveBooks(books)
fmt.Printf("%s successfully!\n", os.Args[1])
}
func handleDeleteBook(deleteCmd *flag.FlagSet, id *string) {
deleteCmd.Parse(os.Args[2:])
if *id == "" {
deleteCmd.PrintDefaults()
log.Fatal("Pls provide id command.")
}
if *id != "" {
var isExist bool
books := getBooks()
for i, book := range books {
if book.Id == *id {
books = append(books[:i], books[i+1:]...)
isExist = true
}
}
if !isExist {
log.Fatal("Book not found!")
}
err := saveBooks(books)
checkError(err)
fmt.Println("Delete successfully!")
}
}
func saveBooks(books []Book) error {
bookBytes, err := json.Marshal(books)
checkError(err)
err = ioutil.WriteFile("./books.json", bookBytes, 0644)
return err
}
func getBooks() (books []Book) {
bookBytes, err := ioutil.ReadFile("./books.json")
checkError(err)
err = json.Unmarshal(bookBytes, &books)
checkError(err)
return books
}
func checkError(err error) {
if err != nil {
log.Fatal("Error happended ", err)
}
}