參考韓順平老師教程,自己摸索出來的適合go入門的mvc小項目。
文件結構如下:
main.go爲入口文件,modelCustomer.go,customerView.go,serviceCustomer.go分別爲mvc文件。
main.go:
package main
import (
"learn/customerManage/view"
)
func main() {
view.GetCustomerView().MainMenu()
}
modelCustomer.go:
package model
type Customer struct {
Id int
Name string
Sex string
Age int
Phone string
Email string
}
func (this *Customer) Add(name string, sex string, age int, phone string, email string) *Customer {
this.Name = name
this.Sex = sex
this.Age = age
this.Phone = phone
this.Email = email
return this
}
func (this *Customer) AddWithInt(id int, name string, sex string, age int, phone string, email string) *Customer {
this.Id = id
this.Name = name
this.Sex = sex
this.Age = age
this.Phone = phone
this.Email = email
return this
}
customerView.go:
package view
import (
"fmt"
"learn/customerManage/service"
)
type CustomerView struct {
key int
service *service.ServiceCustomer
}
func GetCustomerView() CustomerView {
return CustomerView{}
}
func (this *CustomerView) Edit() {
id := 0
name := ""
age := 0
sex := ""
phone := ""
email := ""
this.List()
fmt.Println("請輸入要編輯的客戶id")
fmt.Scanln(&id)
fmt.Println("請輸入姓名")
fmt.Scanln(&name)
fmt.Println("請輸入年齡")
fmt.Scanln(&age)
fmt.Println("請輸入性別")
fmt.Scanln(&sex)
fmt.Println("請輸入手機")
fmt.Scanln(&phone)
fmt.Println("請輸入郵箱")
fmt.Scanln(&email)
this.service.Edit(id, name, sex, age, phone, email)
fmt.Println("編輯成功")
}
func (this *CustomerView) Del() {
this.List()
fmt.Println("請輸入要刪除的顧客id")
id := 0
fmt.Scanln(&id)
this.service.Del(id)
fmt.Println("刪除成功")
}
func (this *CustomerView) Add() {
name := ""
age := 0
sex := ""
phone := ""
email := ""
fmt.Println("請輸入姓名")
fmt.Scanln(&name)
fmt.Println("請輸入年齡")
fmt.Scanln(&age)
fmt.Println("請輸入性別")
fmt.Scanln(&sex)
fmt.Println("請輸入手機")
fmt.Scanln(&phone)
fmt.Println("請輸入郵箱")
fmt.Scanln(&email)
this.service.Add(name, sex, age, phone, email)
fmt.Println("新增成功!")
}
func (this *CustomerView) List() {
list := this.service.List()
fmt.Println("Id\tName\tSex\tAge\tPhone\tEmail")
for _, customer := range list {
fmt.Printf("%v\t%v\t%v\t%v\t%v\t%v\n", customer.Id, customer.Name, customer.Sex, customer.Age, customer.Phone, customer.Email)
}
}
func (this CustomerView) MainMenu() {
service := service.GetServiceCustomer()
this.service = service
labelBreak:
for {
fmt.Println("-------------客戶信息管理軟件--------------")
fmt.Println(" 1 添加客戶")
fmt.Println(" 2 修改客戶")
fmt.Println(" 3 刪除客戶")
fmt.Println(" 4 客戶列表")
fmt.Println(" 5 退 出")
fmt.Println("請選擇:1-5")
fmt.Scanln(&this.key)
switch this.key {
case 1 :
this.Add()
case 2 :
this.Edit()
case 3 :
this.Del()
case 4 :
this.List()
case 5 :
break labelBreak
default :
fmt.Println("輸入有誤。。。")
}
}
}
serviceCustomer.go:
package service
import (
"learn/customerManage/model"
"fmt"
)
type ServiceCustomer struct {
CustomerNum int
Customers []*model.Customer
}
func GetServiceCustomer() *ServiceCustomer {
return &ServiceCustomer{CustomerNum : 0}
}
func (this *ServiceCustomer) Edit(id int, name string, sex string, age int, phone string, email string) {
boo := false
for _, customer := range this.Customers {
if customer.Id == id {
boo = true
this.Del(id)
}
}
if !boo {
fmt.Println("請檢查輸入的客戶id,未找到相關客戶")
return
}
this.AddWithInt(id, name , sex, age, phone, email)
}
func (this *ServiceCustomer) Del(id int) {
var customers []*model.Customer
for _, customer := range this.Customers {
if customer.Id != id {
customers = append(customers, customer)
}
}
this.Customers = customers
}
func (this *ServiceCustomer) AddWithInt(id int, name string, sex string, age int, phone string, email string) {
customer := &model.Customer{}
customer.AddWithInt(id, name, sex, age, phone, email)
this.Customers = append(this.Customers, customer)
}
func (this *ServiceCustomer) Add(name string, sex string, age int, phone string, email string) {
this.CustomerNum += 1
customer := model.Customer{
Id : this.CustomerNum,
}
cusNew := (&customer).Add(name, sex, age, phone, email)
this.Customers = append(this.Customers, cusNew)
}
func (this *ServiceCustomer) List() []*model.Customer {
if this.CustomerNum < 1 {//沒有數據添加一行
this.CustomerNum = 1
customer := &model.Customer{
Id : this.CustomerNum,
Name : "zdd",
Sex : "man",
Age : 28,
Phone : "15900000000",
Email : "[email protected]",
}
this.Customers = append(this.Customers, customer)
}
return this.Customers
}
終端效果如下: