简单学生管理
package main
import "fmt"
var (
allStudent map[int64]*student
)
type student struct {
id int64
name string
}
func newStudent(id int64, name string) *student {
return &student{
id: id,
name: name,
}
}
func showAllStudent() {
for k, v := range allStudent {
fmt.Printf("学号:%d 姓名:%s\n", k, v.name)
}
}
func addStudent() {
var id int64
var name string
fmt.Println("输入ID:")
fmt.Scanln(&id)
fmt.Println("输入学生名:")
fmt.Scanln(&name)
stu := newStudent(id, name)
allStudent[id] = stu
}
func removeStudent() {
var id int64
fmt.Println("输入ID:")
fmt.Scanln(&id)
fmt.Println("删除:", allStudent[id])
delete(allStudent, id)
}
func main() {
allStudent = make(map[int64]*student)
for {
fmt.Println(`
1.查看所有学生
2.添加学生
3.删除学生
4.退出
`)
var ch int8
fmt.Print("\n\n请选择:")
fmt.Scanln(&ch)
fmt.Printf("您选择了:%d\n", ch)
switch ch {
case 1:
showAllStudent()
case 2:
addStudent()
case 3:
removeStudent()
case 4:
return
default:
continue
}
}
}