标签:write def 文件 port 成功 选项 数据 date str
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type Student struct {
ID int
Age int
Score int
Name string
}
type Class struct {
ID int
Student map[int]*Student
}
func (c *Class) showStudent() {
fmt.Printf("\t%s\t%s\t%s\t%s\n", "ID", "姓名", "年龄", "分数")
sortId := make([]int, 0)
for k := range c.Student {
sortId = append(sortId, k)
}
for _, v := range sortId {
studentMsg := c.Student[v]
fmt.Println(studentMsg)
fmt.Printf("\t%s\t%s\t%s\t%s\n", studentMsg.ID, studentMsg.Name, studentMsg.Age, studentMsg.Score)
}
}
func (c *Class) addStudent() {
var id, age, score int
var name string
fmt.Print("请输入id:")
_, err := fmt.Scan(&id)
fmt.Print("请输入name:")
_, err = fmt.Scan(&name)
fmt.Print("请输入age:")
_, err = fmt.Scan(&age)
fmt.Print("请输入score:")
_, err = fmt.Scan(&score)
if err != nil {
fmt.Println("输入有无,保存信息错误")
}
_, isSave := c.Student[id]
if isSave {
fmt.Println("学生ID已存在,请重新输入")
}
student := &Student{
ID: id,
Name: name,
Age: age,
Score: score,
}
// 将输入内容存入内存
c.ID = id
c.Student[id] = student
// 将map类型转换成string类型,并写入txt文件
dataType, _ := json.Marshal(*student)
dataString := string(dataType)
writeFile(dataString+"\n")
fmt.Println("添加成功")
fmt.Println(&c)
}
func (c *Class) delStudent() {
fmt.Println("请输入用户ID:")
var id int
_, err := fmt.Scan(&id)
if err != nil {
fmt.Println("输入有误,请重新输入")
}
_, isSave := c.Student[id]
if !isSave {
fmt.Println("用户ID不存在")
return
}
delete(c.Student, id)
fmt.Println("删除成功")
}
func (c *Class) updateStudent() {
fmt.Println("请输入用户ID:")
var id int
_, err := fmt.Scan(&id)
if err != nil {
fmt.Println("输入有误")
return
}
_, isSave := c.Student[id]
if !isSave {
fmt.Println("用户不存在,请重新输入")
return
}
var name string
var age int
var score int
fmt.Println("请输入新的name:")
_, err = fmt.Scan(&name)
fmt.Println("请输入新的age:")
_, err = fmt.Scan(&age)
fmt.Println("请输入新的score:")
_, err = fmt.Scan(&score)
student := &Student{
ID: id,
Name: name,
Age: age,
Score: score,
}
c.Student[id] = student
fmt.Println("修改成功")
}
func readFile() {
content, err := ioutil.ReadFile("./studentMsg.txt")
if err != nil {
fmt.Println("read file failed, err:", err)
return
}
fmt.Println("文件内容如下:")
fmt.Print(string(content))
}
func writeFile(name string) {
file, err := os.OpenFile("./studentMsg.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
fmt.Println("open file failed, err:", err)
return
}
defer file.Close()
file.Write([]byte(name+"\n")) //写入字节切片数据
//file.WriteString(name+"\n") //直接写入字符串数据
fmt.Println("开始写入文件")
}
func main() {
c := &Class{}
c.Student = make(map[int]*Student, 50)
for {
fmt.Println("学生管理系统:")
fmt.Println()
fmt.Println("1.查看学生 \n2.添加学生 \n3.删除学生 \n4.修改学生信息 \n5.查看文件 \n6.退出登陆")
fmt.Println()
fmt.Print("请输入选项: ")
var num int
_, err := fmt.Scan(&num)
if err != nil {
fmt.Println("输入有无,请重新输入")
}
switch num {
case 1:
c.showStudent()
case 2:
c.addStudent()
case 3:
c.delStudent()
case 4:
c.updateStudent()
case 5:
readFile()
case 6:
goto goHome
default:
fmt.Println("输入有无,请重新输入")
continue
}
}
goHome:
fmt.Println("退出系统")
}
标签:write def 文件 port 成功 选项 数据 date str
原文地址:https://www.cnblogs.com/ipyanthony/p/14600624.html