总计架构图:
model/customer.go
package model import ( "fmt" ) type Customer struct { Id int Name string Gender Age Phone Email } func NewCustomer(id int,name string,gender ) Customer { return Customer{ Id: id,Name: name,Gender: gender,Age: age,Phone: phone,Email: email,} } func NewCustomer2(name Customer{ Name: name,} } func (c Customer) GetInfo() { 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) info }
service/customerService.go
package service import ( go_code/project_6/model ) //增删查改 type CustomerService { customers []model.Customer customerId } func NewCustomerService() *CustomerService { customerService := &CustomerService{} customerService.customerId = 1 customer := model.NewCustomer(1,张三",1)">男20,1)">15927776543347688971@qq.com) customerService.customers = append(customerService.customers,customer) customerService } func (cs *CustomerService) List() []model.Customer { cs.customers } func (cs *CustomerService) Add(customer model.Customer) bool { 确定一个添加id的规则,就是添加的顺序 cs.customerId++ customer.Id = cs.customerId cs.customers = append(cs.customers,1)">return true } 根据Id查找客户在切片中的对应下标,如果没有则返回-1 func (cs *CustomerService) FindById(id int) { index := -1 for i := 0; i < len(cs.customers); i++ { if cs.customers[i].Id == id { 找到了 index = i } } index } 根据Id删除客户 func (cs *CustomerService) Delete(id { index := cs.FindById(id) if index == -false } 从切片中删除元素 cs.customers = append(cs.customers[:index],cs.customers[index+:]...) } func (cs *CustomerService) GetinfoById(id ) model.Customer { i := id - cs.customers[i] } 根据id修改客户信息 func (cs *CustomerService) Update(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 } } }
view/customerView.go
package main import ( " go_code/project_6/service ) type customerView 定义必要的字段 key string 接受用户输入 loop bool 用于循环判断 customerService *service.CustomerService } func (cv *customerView) mainMenu() { for { fmt.Println(------------------客户信息管理软件---------------------) fmt.Println( 1.添加客户 2.修改客户 3.删除客户 4.客户列表 5.退 出) fmt.Print(请选择1-5:) fmt.Scanln(&cv.key) switch cv.key { case 1: cv.add() 2: cv.update() 3: cv.delete() 4: cv.list() 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邮箱) 0; i < len(customers); i++ { fmt.Println(customers[i].GetInfo()) } fmt.Println(------------------客户列表完成------------------------customerView) add() { fmt.Println(--------------------添加客户--------------------------) fmt.Print(姓名:) name := "" fmt.Scanln(&name) fmt.Print(性别:) gender := gender) fmt.Print(年龄:) age := 0age) fmt.Print(电话:) phone := phone) fmt.Print(邮箱:) email := email) customer := model.NewCustomer2(name,gender,age,phone,email) if cv.customerService.Add(customer) { fmt.Println(--------------------添加成功--------------------------) } else--------------------添加失败--------------------------) } fmt.Println(--------------------添加完成--------------------------) } func (cv *customerView) () { fmt.Println(--------------------删除客户--------------------------请选择要删除客户的客户编号(-1退出):) id := -id) if id == - } fmt.Println(确认是否删除?y/n) choice := choice) if choice == y" || choice == n cv.customerService.Delete(id) { fmt.Println(--------------------删除完成--------------------------) } { fmt.Println(-----------------输入id不存在,请重新输入--------------) } } } func (cv *customerView) update() { fmt.Print(请输入要修改的id:) id := if cv.customerService.FindById(id) != - { customer := cv.customerService.GetinfoById(id) fmt.Printf(姓名(%v:) fmt.Scanln(&name) fmt.Printf(性别(%v):gender) fmt.Printf(年龄(%v):age) fmt.Printf(电话(%v):phone) fmt.Printf(邮箱(%v):email) customer2 :=) } } func (cv *customerView) logout() { fmt.Println(确认是否退出?y/n { fmt.Scanln(&if cv.key == " || cv.key == { } fmt.Println(你的输入有误,请从新输入) } { cv.loop = } } func main() { var cv customerView cv.loop = cv.customerService = service.NewCustomerService() cv.mainMenu() }
由于代码都比较基础,就不一一介绍了,很容易看懂。我们运行程序:
先选择4:我们已经初始化了一条数据。
再选择1添加一条数据:
再选择4查看一下:
数据确实已经添加成功,我们再选择3,删除一条数据:
再查看一下:
确实已经删除,然后我们选择2修改数据:
再查看一下:
已经修改了,最后我们选择5进行退出:
总结:通过golang实现的客户信息管理系统。学习一门语言最好的方式就是通过一个实际的例子。通过这个实例,不仅可以进一步巩固golang的相关基础技能,同时,也能让我们加强自己的逻辑能力,从一步步的调用函数,掌握参数传递和接收技巧。