student.go
package main
import "fmt"
type student struct {
id int64
name string
}
type studentMgr struct {
allStudent map[int64]student
}
func (sm studentMgr) showStudent() {
for _, v := range sm.allStudent {
fmt.Printf("%d %s", v.id, v.name)
}
}
func (s studentMgr) addStudent() {
var (
id int64
name string
)
fmt.Println("please input your id")
fmt.Scanln(&id)
fmt.Println("please input your name")
fmt.Scanln(&name)
newstu := student{id, name}
s.allStudent[id] = newstu
}
func (s studentMgr) delStudent() {
s.showStudent()
var (
id int64
)
fmt.Println("please input your id")
fmt.Scanln(&id)
b, ok := s.allStudent[id]
if !ok {
fmt.Print("have some issue in checking stu")
} else {
fmt.Println(b.id, b.name)
}
delete(s.allStudent, id)
}
func (s studentMgr) update() {
s.showStudent()
var (
id int64
name string
)
fmt.Println("please input your id")
fmt.Scanln(&id)
b, ok := s.allStudent[id]
if !ok {
fmt.Print("have some issue")
} else {
fmt.Println(b.id, b.name)
}
fmt.Println("please input your id")
fmt.Scanln(&name)
b.name = name
s.allStudent[id] = b
}
main.go
package main
import (
"fmt"
)
var smr studentMgr
func showMenu() {
fmt.Println(`
welcome to the stu manage
1.show student
2.add
3.del stu
4.update
`)
}
func main() {
var choice int
smr = studentMgr{
allStudent: make(map[int64]student, 20),
}
for {
showMenu()
fmt.Print("please inout number")
fmt.Scanln(&choice)
fmt.Println("your input is ")
switch choice {
case 1:
smr.showStudent()
case 2:
smr.addStudent()
case 3:
smr.delStudent()
case 4:
smr.update()
}
}
}