Golang Program to Create an Interface Named Cache that Defines a Set and Get Method


The purpose of this interface is to provide a contract for implementing cache functionality in various data structures or systems. The Set method is responsible for storing a value in the cache with a specified key, while the Get method retrieves the value associated with a given key from the cache. In this article, we will create an interface called Cache that defines two methods: Set and Get. Here we are going to use two different methods − Get(key string) interface{} as well as Set(key string, value interface{}) along with examples to elaborate the concept.

Syntax

Set(key string, value interface{})

By using this Syntax, any type that implements the Cache interface will need to provide a method called Set that takes a string key and a value of any type as input and performs the necessary operations to store the value in the cache.

Get(key string) interface{}

By using this Syntax, any type that implements the Cache interface will need to provide a method called Get that takes a string key as input and returns a value of any type. The implementation of the Get method will involve retrieving the value associated with the given key from the cache and returning it to the caller.

ioutil.ReadFile(path)

This function is used to read the data that is in the file, and then returns the data back as the byte slice.

path − it is the path to the specific file on which we need to apply the function.

log.Fatalf("Error reading file: %v", err)

This statement in go language is used to print the errors, it then terminates the program after printing the error message.

  • log − it represents a logging package.

  • Fatalf − it is a log package method to log a formatted message.

  • err − it is the variable that contains the error information that has occurred while reading the file.

Example 1

In this example, the Cache interface takes two parameters − a key of type string and a value of type interface{}. The method is dependable for storing the given value within the cache based on the given key. We define a struct named MyCache that calls to a particular usage of the Cache interface. The Set method is processed as a method of the MyCache struct. In the code, we define the Cache interface with two methods: Set and Get. The Set method takes a key of type string and a value of type interface{} as input and stores the value in the cache. The Get method takes a key of type string as input and retrieves the corresponding value from the cache. The Set method adds or updates a value in the map based on the given key, and the Get method retrieves the value associated with the key from the map.In the main function, we create a new instance of MyCache and use it to set and get values in the cache. Finally, we print the retrieved values to verify that they were stored and retrieved correctly.

package main

import "fmt"

type Cache interface {
   Set(key string, value interface{})
   Get(key string) interface{}
}

type MyCache struct {
   data map[string]interface{}
}

func (c *MyCache) Set(key string, value interface{}) {
   c.data[key] = value
}

func (c *MyCache) Get(key string) interface{} {
   return c.data[key]
}

func main() {
	
   cache := &MyCache{
      data: make(map[string]interface{}),
   }
	
   cache.Set("key1", "value1")
   cache.Set("key2", 42)
	
   value1 := cache.Get("key1")
   value2 := cache.Get("key2")

   fmt.Println(value1) 
   fmt.Println(value2) 
}

Output

value1
42

Example 2

The Get method within the Cache interface takes a key of type string as a parameter and returns an interface{} as a value. This method is dependable for recovering the value related to the given key from the cache.In the code , we define the Cache interface with two methods: Set and Get. The Set method takes a key of type string and a value of type interface{} as input and stores the value in the cache. The Get method takes a key of type string as input and retrieves the corresponding value from the cache.We also provide an example implementation of the Cache interface called MyCache.In the main function, we create a new instance of MyCache and use it to set and get values in the cache. Finally, we print the retrieved values to verify that they were stored and retrieved correctly.

package main

import (
   "fmt"
   "io/ioutil"
   "log"
)

func readFile(filePath string) {
   content, err := ioutil.ReadFile(filePath)
   if err != nil {
      log.Fatalf("Error reading file: %v", err)
   }
   fmt.Printf("File content: %s\n", content)
}

func main() {
   filePath := "/path/to/your/file.txt"
   readFile(filePath)
}

Output

value1
42

Conclusion

In this program, we learned how to form an interface named Cache in Golang, which characterizes a Set and Get a strategy. This interface serves as an outline for actualizing different cache components in our application. By utilizing this interface, we are able to accomplish adaptability and code reusability, as distinctive cache executions can accommodate the Cache interface whereas giving their claim rationale for putting away and recovering information. This approach permits us to effortlessly switch between distinctive cache usage based on our application's necessities.

Updated on: 20-Jul-2023

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements