package entity
// 结构体:人type Person struct{
Id int
Name string
Sex string
Age int}// 构造函数funcNewPerson(id int, name, sex string, age int)*Person {return&Person{
Id: id,
Name: name,
Sex: sex,
Age: age,}}
人员管理结构体
package service
import("demo/entity""fmt")type PersonService struct{
num int// 人员数量
personList []*entity.Person // 人员切片}// 构造函数funcNewPersonService()*PersonService {return&PersonService{
num:0,
personList:make([]*entity.Person,0,100),}}// 1)添加人员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("===========添加成功===========")}// 2)删除人员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不存在,删除失败===========")}// 3)修改人员func(personService *PersonService)UpdatePerson(person *entity.Person){for i, v :=range personService.personList {if person.Id == v.Id {
personService.personList[i]= person
fmt.Println("===========修改成功===========")return}}
fmt.Println("===========人员ID不存在,修改失败===========")}// 4)查询人员func(personService *PersonService)ListPerson(){
fmt.Println("ID","姓名","性别","年龄")for_, v :=range personService.personList {
fmt.Println(v.Id, v.Name, v.Sex, v.Age)}}
题目: 题解:
func minDistance(word1 string, word2 string) int {m, n : len(word1), len(word2)dp : make([][]int, m1)for i : range dp {dp[i] make([]int, n1)}for i : 0; i < m1; i {dp[i][0] i // word1[i] 变成 word2[0], 删掉 word1[i], …