代码结构
人员结构体
package entity
type Person struct { Id int Name string Sex string Age int
}
func NewPerson ( id int , name, sex string , age int ) * Person { return & Person{ Id: id, Name: name, Sex: sex, Age: age, }
}
人员管理结构体
package serviceimport ( "demo/entity" "fmt"
) type PersonService struct { num int personList [ ] * entity. Person
}
func NewPersonService ( ) * PersonService { return & PersonService{ num: 0 , personList: make ( [ ] * entity. Person, 0 , 100 ) , }
}
func ( personService * PersonService) AddPerson ( person * entity. Person) { for _ , v := range personService. personList { if person. Id == v. Id { fmt. Println ( "===========人员ID已存在,添加失败===========" ) return } } personService. personList = append ( personService. personList, person) personService. num += 1 fmt. Println ( "===========添加成功===========" )
}
func ( personService * PersonService) DeletePerson ( id int ) { for i, v := range personService. personList { if id == v. Id { personService. personList = append ( personService. personList[ : i] , personService. personList[ i+ 1 : ] ... ) fmt. Println ( "===========删除成功===========" ) return } } fmt. Println ( "===========人员ID不存在,删除失败===========" )
}
func ( personService * PersonService) UpdatePerson ( person * entity. Person) { for i, v := range personService. personList { if person. Id == v. Id { personService. personList[ i] = personfmt. Println ( "===========修改成功===========" ) return } } fmt. Println ( "===========人员ID不存在,修改失败===========" )
}
func ( personService * PersonService) ListPerson ( ) { fmt. Println ( "ID" , "姓名" , "性别" , "年龄" ) for _ , v := range personService. personList { fmt. Println ( v. Id, v. Name, v. Sex, v. Age) }
}
主函数
package mainimport ( "demo/entity" "demo/service" "fmt" "os"
) func main ( ) { personService := service. NewPersonService ( ) for { showMenu ( ) var command int fmt. Scanf ( "%d\n" , & command) switch command { case 1 : fmt. Println ( "【添加人员】" ) fmt. Println ( "请输入人员的ID、名字、性别、年龄(以空格隔开):" ) p, err := newPersonByScan ( ) if err == nil { personService. AddPerson ( p) } case 2 : fmt. Println ( "【删除人员】" ) fmt. Println ( "请输入人员的ID:" ) var id int _ , err := fmt. Scanln ( & id) if err != nil { fmt. Println ( "输入错误,err:" , err) } personService. DeletePerson ( id) case 3 : fmt. Println ( "【修改人员】" ) fmt. Println ( "请输入人员的ID、名字、性别、年龄(以空格隔开):" ) p, err := newPersonByScan ( ) if err == nil { personService. UpdatePerson ( p) } case 4 : fmt. Println ( "【查询人员】" ) personService. ListPerson ( ) case 5 : fmt. Println ( "【退出系统】" ) fmt. Println ( "Bye!" ) os. Exit ( 0 ) default : fmt. Println ( "指令有误,请重新输入!" ) } }
}
func newPersonByScan ( ) ( * entity. Person, error ) { var ( id int name string sex string age int ) _ , err := fmt. Scanln ( & id, & name, & sex, & age) if err != nil { fmt. Println ( "输入错误,err:" , err) } return entity. NewPerson ( id, name, sex, age) , err
} func showMenu ( ) { fmt. Println ( "********************人员管理系统********************" ) fmt. Println ( "1、添加人员" ) fmt. Println ( "2、删除人员" ) fmt. Println ( "3、修改人员" ) fmt. Println ( "4、查询人员" ) fmt. Println ( "5、退出系统" ) fmt. Println ( "****************************************************" )
}
测试
添加人员
删除人员
修改人员
查询人员