Introduction
The Movie Database program aims to store, manage, and retrieve movie information efficiently. It will allow users to input movie details such as title, genre, release year, and rating. Using Go programming language, we will create a simple database where the user can interact with the stored movie data. This database will be in-memory, providing a platform for adding, viewing, updating, and deleting movies from the system.
Objective
The objective of this program is to demonstrate how to create a basic database using Go. The program will handle operations like adding, viewing, and deleting movie records. This will showcase Go’s capabilities in building efficient and reliable systems for managing data.
Code for Movie Database
package main import ( "fmt" "strings" ) // Movie structure type Movie struct { Title string Genre string ReleaseYear int Rating float32 } // Movie database represented as a slice of Movies var movieDatabase []Movie // Function to add a movie to the database func addMovie(title, genre string, releaseYear int, rating float32) { movie := Movie{Title: title, Genre: genre, ReleaseYear: releaseYear, Rating: rating} movieDatabase = append(movieDatabase, movie) fmt.Println("Movie added successfully!") } // Function to view all movies in the database func viewMovies() { if len(movieDatabase) == 0 { fmt.Println("No movies available in the database.") return } fmt.Println("Movies in the Database:") for i, movie := range movieDatabase { fmt.Printf("%d. %s (%d) - Genre: %s, Rating: %.1f\n", i+1, movie.Title, movie.ReleaseYear, movie.Genre, movie.Rating) } } // Function to delete a movie by its title func deleteMovie(title string) { for i, movie := range movieDatabase { if strings.ToLower(movie.Title) == strings.ToLower(title) { movieDatabase = append(movieDatabase[:i], movieDatabase[i+1:]...) fmt.Println("Movie deleted successfully!") return } } fmt.Println("Movie not found!") } // Main function to interact with the user func main() { var option int for { fmt.Println("\nMovie Database Menu:") fmt.Println("1. Add Movie") fmt.Println("2. View Movies") fmt.Println("3. Delete Movie") fmt.Println("4. Exit") fmt.Print("Choose an option: ") fmt.Scanln(&option) switch option { case 1: var title, genre string var releaseYear int var rating float32 fmt.Print("Enter movie title: ") fmt.Scanln(&title) fmt.Print("Enter genre: ") fmt.Scanln(&genre) fmt.Print("Enter release year: ") fmt.Scanln(&releaseYear) fmt.Print("Enter rating (0-10): ") fmt.Scanln(&rating) addMovie(title, genre, releaseYear, rating) case 2: viewMovies() case 3: var titleToDelete string fmt.Print("Enter movie title to delete: ") fmt.Scanln(&titleToDelete) deleteMovie(titleToDelete) case 4: fmt.Println("Exiting the program.") return default: fmt.Println("Invalid option, please try again.") } } }
Explanation of the Program Structure
The Movie Database program is structured as follows:
- Movie Structure: The Movie struct holds the details of each movie such as title, genre, release year, and rating.
- Movie Database: The movieDatabase variable holds a slice of Movie structs, which represents our in-memory database.
- Functions:
- addMovie: Adds a new movie to the database.
- viewMovies: Displays all the movies currently stored in the database.
- deleteMovie: Deletes a movie from the database by title.
- Main Function: Provides a menu for the user to choose options such as adding, viewing, deleting movies, or exiting the program.
How to Run the Program
To run the program, follow these steps:
- Install Go on your system (if not installed already) from here.
- Save the provided Go code in a file, for example
movie_database.go
. - Open your terminal or command prompt and navigate to the directory where the file is saved.
- Run the program by typing
go run movie_database.go
in the terminal. - Interact with the program through the menu options provided.