總計架構圖:
model/customer.go
package model import ( "fmt" ) type Customer struct { Id int Name string Gender string Age int Phone string Email string } func NewCustomer(id int, name string, gender string, age int, phone string, email string) Customer { return Customer{ Id: id, Name: name, Gender: gender, Age: age, Phone: phone, Email: email, } } func NewCustomer2(name string, gender string, age int, phone string, email string) Customer { return Customer{ Name: name, Gender: gender, Age: age, Phone: phone, Email: email, } } func (c Customer) GetInfo() string { info := fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v\t", c.Id, c.Name, c.Gender, c.Age, c.Phone, c.Email) return info }
service/customerService.go
package service import ( "go_code/project_6/model" ) //增刪查改 type CustomerService struct { customers []model.Customer customerId int } func NewCustomerService() *CustomerService { customerService := &CustomerService{} customerService.customerId = 1 customer := model.NewCustomer(1, "張三", "男", 20, "15927776543", "347688971@qq.com") customerService.customers = append(customerService.customers, customer) return customerService } func (cs *CustomerService) List() []model.Customer { return cs.customers } func (cs *CustomerService) Add(customer model.Customer) bool { //確定一個添加id的規則,就是添加的順序 cs.customerId++ customer.Id = cs.customerId cs.customers = append(cs.customers, customer) return true } //根據Id查找客戶在切片中的對應下標,如果沒有則返回-1 func (cs *CustomerService) FindById(id int) int { index := -1 for i := 0; i < len(cs.customers); i++ { if cs.customers[i].Id == id { //找到了 index = i } } return index } //根據Id刪除客戶 func (cs *CustomerService) Delete(id int) bool { index := cs.FindById(id) if index == -1 { return false } //從切片中刪除元素 cs.customers = append(cs.customers[:index], cs.customers[index+1:]...) return true } func (cs *CustomerService) GetinfoById(id int) model.Customer { i := id - 1 return cs.customers[i] } //根據id修改客戶信息 func (cs *CustomerService) Update(id int, customer model.Customer) bool { for i := 0; i < len(cs.customers); i++ { if cs.customers[i].Id == id { cs.customers[i].Name = customer.Name cs.customers[i].Gender = customer.Gender cs.customers[i].Age = customer.Age cs.customers[i].Phone = customer.Phone cs.customers[i].Email = customer.Email } } return true }
view/customerView.go
package main import ( "fmt" "go_code/project_6/model" "go_code/project_6/service" ) type customerView struct { //定義必要的字段 key string //接受用戶輸入 loop bool //用於循環判斷 customerService *service.CustomerService } func (cv *customerView) mainMenu() { for { fmt.Println("------------------客戶信息管理軟件---------------------") fmt.Println(" 1.添加客戶") fmt.Println(" 2.修改客戶") fmt.Println(" 3.刪除客戶") fmt.Println(" 4.客戶列表") fmt.Println(" 5.退 出") fmt.Print("請選擇1-5:") fmt.Scanln(&cv.key) switch cv.key { case "1": cv.add() case "2": cv.update() case "3": cv.delete() case "4": cv.list() case "5": cv.logout() default: fmt.Println("你的輸入有誤,請重新輸入") } if !cv.loop { break } } fmt.Println("你已退出了客戶關系管理系統。。。") } func (cv *customerView) list() { //首先獲取當前客戶所有信息 customers := cv.customerService.List() fmt.Println("--------------------客戶列表--------------------------") fmt.Println("編號\t姓名\t性別\t年齡\t電話\t\t郵箱") for i := 0; i < len(customers); i++ { fmt.Println(customers[i].GetInfo()) } fmt.Println("------------------客戶列表完成------------------------") } func (cv *customerView) add() { fmt.Println("--------------------添加客戶--------------------------") fmt.Print("姓名:") name := "" fmt.Scanln(&name) fmt.Print("性別:") gender := "" fmt.Scanln(&gender) fmt.Print("年齡:") age := 0 fmt.Scanln(&age) fmt.Print("電話:") phone := "" fmt.Scanln(&phone) fmt.Print("郵箱:") email := "" fmt.Scanln(&email) customer := model.NewCustomer2(name, gender, age, phone, email) if cv.customerService.Add(customer) { fmt.Println("--------------------添加成功--------------------------") } else { fmt.Println("--------------------添加失敗--------------------------") } fmt.Println("--------------------添加完成--------------------------") } func (cv *customerView) delete() { fmt.Println("--------------------刪除客戶--------------------------") fmt.Println("請選擇要刪除客戶的客戶編號(-1退出):") id := -1 fmt.Scanln(&id) if id == -1 { return } fmt.Println("確認是否刪除?y/n") choice := "" fmt.Scanln(&choice) if choice == "y" || choice == "n" { if cv.customerService.Delete(id) { fmt.Println("--------------------刪除完成--------------------------") } else { fmt.Println("-----------------輸入id不存在,請重新輸入--------------") } } } func (cv *customerView) update() { fmt.Print("請輸入要修改的id:") id := 0 fmt.Scanln(&id) if cv.customerService.FindById(id) != -1 { customer := cv.customerService.GetinfoById(id) fmt.Printf("姓名(%v:)", customer.Name) name := "" fmt.Scanln(&name) fmt.Printf("性別(%v):", customer.Gender) gender := "" fmt.Scanln(&gender) fmt.Printf("年齡(%v):", customer.Age) age := 0 fmt.Scanln(&age) fmt.Printf("電話(%v):", customer.Phone) phone := "" fmt.Scanln(&phone) fmt.Printf("郵箱(%v):", customer.Email) email := "" fmt.Scanln(&email) customer2 := model.NewCustomer2(name, gender, age, phone, email) cv.customerService.Update(id, customer2) } else { fmt.Println("-----------------輸入id不存在,請重新輸入--------------") } } func (cv *customerView) logout() { fmt.Println("確認是否退出?y/n") for { fmt.Scanln(&cv.key) if cv.key == "y" || cv.key == "n" { break } fmt.Println("你的輸入有誤,請從新輸入") } if cv.key == "y" { cv.loop = false } } func main() { var cv customerView cv.loop = true cv.customerService = service.NewCustomerService() cv.mainMenu() }
由於代碼都比較基礎,就不一一介紹了,很容易看懂。我們運行程序:
先選擇4:我們已經初始化了一條數據。
再選擇1添加一條數據:
再選擇4查看一下:
數據確實已經添加成功,我們再選擇3,刪除一條數據:
再查看一下:
確實已經刪除,然后我們選擇2修改數據:
再查看一下:
已經修改了,最后我們選擇5進行退出:
總結:通過golang實現的客戶信息管理系統。學習一門語言最好的方式就是通過一個實際的例子。通過這個實例,不僅可以進一步鞏固golang的相關基礎技能,同時,也能讓我們加強自己的邏輯能力,從一步步的調用函數,掌握參數傳遞和接收技巧。