当前位置:网站首页>[go language learning] structure practice
[go language learning] structure practice
2022-06-09 16:18:00 【Wenwenal】
️ Write it at the front
- This is wenwener's way of learning
- If it helps you , Give the blogger a free praise to encourage QAQ
- Blog home page Wenwenell's study cabin
- ️ For more articles, please pay attention to wenwenwener's home page
- Article release date :2022.05.025
- java Way of learning !
- You are welcome to comment on the collection ️
- To rush
- ️ Last article :【GO The road to language learning 】GO Variables in language
List of articles
Actual target , Using structure to realize the
- increase
- modify
- Inquire about
package main
import "fmt"
// Student information management system
/** 1. Students have id、 full name 、 Age 、 Scores and other information 2. The program provides a list of students 、 Add student 、 Edit student information 、 Delete students and other functions */
type Student struct {
id int
name string
class string
}
func newStudent(id int, name string, class string) *Student {
return &Student{
id: id,
name: name,
class: class,
}
}
type studentMgr struct {
allStudents []*Student
}
func newStudentMgr() *studentMgr {
return &studentMgr{
allStudents: make([]*Student, 0, 100),
}
}
// Add student
func (s *studentMgr) addStudent(stu *Student) {
s.allStudents = append(s.allStudents, stu)
}
// Editing students
func (s *studentMgr) editStudent(stu *Student) {
for i, v := range s.allStudents {
if v.id == stu.id {
s.allStudents[i] = stu
return
}
}
// If you go here, you can't find the designated student
fmt.Printf(" The student information entered is incorrect , There is no student number in the system %d Of the students \n", stu.id)
}
// Delete students
func (s *studentMgr) deleteStudent(stu *Student) {
for i, v := range s.allStudents {
if v.id == stu.id {
s.allStudents = append(s.allStudents[:i], s.allStudents[i+1:]...)
return
}
}
// If you go here, you can't find the designated student
fmt.Printf(" The student information entered is incorrect , There is no student number in the system %d Of the students \n", stu.id)
}
// Show students
func (s *studentMgr) showStudent() {
for _, v := range s.allStudents {
fmt.Printf(" Student number :%d full name : %s class : %s\n", v.id, v.name, v.class)
}
}
package main
import (
"fmt"
"os"
)
// Get user input function
func getInput() *Student {
var (
id int
name string
class string
)
fmt.Println(" Please enter student information as required ")
fmt.Print(" Please enter the student number of the student :")
fmt.Scanf("%d\n", &id)
fmt.Print(" Please enter the student's name :")
fmt.Scanf("%s\n", &name)
fmt.Print(" Please enter the student's class :")
fmt.Scanf("%s\n", &class)
return newStudent(id, name, class)
}
func main() {
sm := newStudentMgr()
for {
//1. Print system menu
showMenu()
//2. Wait for the user to select the option
var input int
fmt.Println(" Please enter the serial number you want to operate ")
fmt.Scanf("%d\n", &input)
fmt.Println(" User input is ", input)
//3. Execute the action selected by the user
switch input {
case 1:
// Add students
sm.addStudent(getInput())
fmt.Println(" Add success ")
case 2:
// Edit student information
sm.editStudent(getInput())
fmt.Println(" Edit success ")
case 3:
// Show all student information
sm.showStudent()
case 4:
// Delete student information
sm.deleteStudent(getInput())
fmt.Println(" Delete successful ")
case 5:
// Exit the system
os.Exit(0)
}
}
}
func showMenu() {
fmt.Println(" Welcome to the student information management system ")
fmt.Println("1. Add students ")
fmt.Println("2. Edit student information ")
fmt.Println("3. Show all student information ")
fmt.Println("4. Delete student information ")
fmt.Println("5. Exit the system ")
}
test
add to
Welcome to the student information management system
1. Add students
2. Edit student information
3. Show all student information
4. Delete student information
5. Exit the system
Please enter the serial number you want to operate
1
User input is 1
Please enter student information as required
Please enter the student number of the student :100
Please enter the student's name :zs
Please enter the student's class :201913
Add success
Welcome to the student information management system
1. Add students
2. Edit student information
3. Show all student information
4. Delete student information
5. Exit the system
Please enter the serial number you want to operate
1
User input is 1
Please enter student information as required
Please enter the student number of the student :101
Please enter the student's name :ls
Please enter the student's class :201914
Add success
Delete
Welcome to the student information management system
1. Add students
2. Edit student information
3. Show all student information
4. Delete student information
5. Exit the system
Please enter the serial number you want to operate
4
User input is 4
Please enter student information as required
Please enter the student number of the student :100
Please enter the student's name :zs
Please enter the student's class :201913
Delete successful
modify
Please enter the serial number you want to operate
3
User input is 3
Student number :101 full name : ls class : 201914
Welcome to the student information management system
1. Add students
2. Edit student information
3. Show all student information
4. Delete student information
5. Exit the system
Please enter the serial number you want to operate
2
User input is 2
Please enter student information as required
Please enter the student number of the student :101
Please enter the student's name :zs
Please enter the student's class :201913
Edit success
Welcome to the student information management system
1. Add students
2. Edit student information
3. Show all student information
4. Delete student information
5. Exit the system
Please enter the serial number you want to operate
3
User input is 3
Student number :101 full name : zs class : 201913
Inquire about
Welcome to the student information management system
1. Add students
2. Edit student information
3. Show all student information
4. Delete student information
5. Exit the system
Please enter the serial number you want to operate
3
User input is 3
Student number :101 full name : zs class : 201913
边栏推荐
- 在Flutter中自定义应用程序内键盘
- R语言ggplot2可视化:使用stat_summary函数在ggplot2可视化图像的结果中添加样本个数信息(stat_summary to annotate plot sample number)
- flutter系列之:Material主题的基础-MaterialApp
- 618's money saving technology strategy is coming - experience the scene and get a 10 yuan cat super card!
- Kubernetes core concepts
- leetcode:240.搜索二维矩阵 II
- R language generalized linear model function GLM, GLM function to build logistic regression model, analyze whether the model is over dispersed, and use hypothesis test to analyze whether the model is
- 关闭StackExchange等平台的privacy收集窗口
- 五月集训(第25天) —— 树状数组
- opensuse 自动挂载硬盘
猜你喜欢

LeetCode 6077. The total power of wizards and

在Flutter中自定义应用程序内键盘

Openwrt firewall

GoLand运行go程序时working directory的设置问题:报错路径找不到no such file or directory时需检查该配置

618. How to prepare for the great promotion

大人,时代变了 。。。

测试必看,初次编写测试用例的要点

65 2D drawing (basic drawing and filling)

Conference control keyboard instructions for ≥ 3.5-inch LCD screen

Customizing the in app keyboard in fluent
随机推荐
Win10找不到飞行模式开关怎么办?
资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员
Openwrt firewall
技术 - 中台
How does the memory database give full play to its memory advantage?
Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation
GoLand运行go程序时working directory的设置问题:报错路径找不到no such file or directory时需检查该配置
R language generalized linear model function GLM, GLM function to build logistic regression model, analyze whether the model is over dispersed, and use hypothesis test to analyze whether the model is
30岁 思维导图
Kubernetes 核心概念
65 2D drawing (basic drawing and filling)
合约安全之-变量隐藏安全问题分析
June training (day 05) - double pointer
Deepin 运行 cherrytree报错找不到模块gtksourceview2
May training (the 26th day) - collective search
618's money saving technology strategy is coming - experience the scene and get a 10 yuan cat super card!
virtualBox 虚拟机网卡设置
应用软件效率测试的执行策略
LINQ left join example
dotnet core 发布只带必要的依赖文件