当前位置:网站首页>[go language learning] structure practice

[go language learning] structure practice

2022-06-09 16:18:00 Wenwenal

️ Write it at the front



List of articles


Actual target , Using structure to realize the

  1. increase
  2. modify
  3. 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

原网站

版权声明
本文为[Wenwenal]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091544115506.html