Let's create a basic Student Management System in Go (also known as Golang) that provides functionalities for CRUD (create student, read student, update student, and delete student) operations.
Source Code: Student Management System in Golang
package main
import (
"fmt"
)
type Student struct {
RollNo int
Name string
Marks float64
}
var students = make(map[int]Student)
func addStudent(rollNo int, name string, marks float64) {
students[rollNo] = Student{RollNo: rollNo, Name: name, Marks: marks}
}
func displayStudents() {
for _, student := range students {
fmt.Printf("Roll No: %d, Name: %s, Marks: %f\n", student.RollNo, student.Name, student.Marks)
}
}
func updateStudent(rollNo int, name string, marks float64) {
if _, exists := students[rollNo]; exists {
students[rollNo] = Student{RollNo: rollNo, Name: name, Marks: marks}
} else {
fmt.Println("Student not found!")
}
}
func deleteStudent(rollNo int) {
if _, exists := students[rollNo]; exists {
delete(students, rollNo)
} else {
fmt.Println("Student not found!")
}
}
func main() {
var choice, rollNo int
var name string
var marks float64
for {
fmt.Println("\n1. Add Student\n2. Display Students\n3. Update Student\n4. Delete Student\n5. Exit")
fmt.Print("Enter your choice: ")
fmt.Scan(&choice)
switch choice {
case 1:
fmt.Print("Enter roll number, name, and marks: ")
fmt.Scan(&rollNo, &name, &marks)
addStudent(rollNo, name, marks)
case 2:
displayStudents()
case 3:
fmt.Print("Enter roll number to update: ")
fmt.Scan(&rollNo)
fmt.Print("Enter new name and marks: ")
fmt.Scan(&name, &marks)
updateStudent(rollNo, name, marks)
case 4:
fmt.Print("Enter roll number to delete: ")
fmt.Scan(&rollNo)
deleteStudent(rollNo)
case 5:
return
}
}
}
Explanation:
We define a Student struct that will hold each student's details. We utilize a map called students where the key is the roll number (unique for each student) and the value is a Student struct.
Functions addStudent, displayStudents, updateStudent, and deleteStudent implement the CRUD functionalities.
In the main() function, we have a loop to provide a menu-driven approach for the user to interact with the system.
Output:
When executed, the program will show a menu, allowing the user to:
- Add a student by inputting roll number, name, and marks.
- Display all students' details.
- Update a student's name and marks using the roll number.
- Delete a student using the roll number.
- Exit the application.
Upon each operation, the user will be redirected back to the main menu until they decide to exit.
Here is the complete output:
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 1
Enter roll number, name, and marks: 100 John 50
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 2
Roll No: 100, Name: John, Marks: 50.000000
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 3
Enter roll number to update: 100
Enter new name and marks: Tom 80
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 4
Enter roll number to delete: 100
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 5
This Student Management System provides a simple example of CRUD operations in Go. Adjustments and enhancements can be made based on your specific requirements.
Happy Coding!.
Comments
Post a Comment