Golang

 

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:

  1. Install Go on your system (if not installed already) from here.
  2. Save the provided Go code in a file, for example movie_database.go.
  3. Open your terminal or command prompt and navigate to the directory where the file is saved.
  4. Run the program by typing go run movie_database.go in the terminal.
  5. Interact with the program through the menu options provided.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)