Golang Program to Lookup enum by String value


In golang, the enumeration constant that corresponds to a given string is necessary to look up an enumeration (enum) by its string value. A switch statement or a string of if-else statements can be used to do this. An alternative method is to use a map, with the enumeration constants serving as the values and the string values acting as the keys. Enumeration constants can now be quickly and effectively looked up using their string form. Let’s find how to lookup enum by string value in go programming language with various example.

Method 1: Using string representation

In this example we will see how to lookup enum by string value using string representation. The const keyword is used to define the Cloth type as an enumeration containing the values Saree, Kurti, and Shrug. The string representations of the clothes are then connected to the enumeration values in a map. The main function searches the clothes map for the enumeration value of "saree" and prints it to the terminal. Let’s see it through Example and algorithm to know its execution.

Syntax

Map[]

A map is a pre-built data structure in Golang that enables key-value pair storage and retrieval. A map's keys need to be distinct, and its values can be of any data type. Data may be efficiently stored and retrieved using maps, which are implemented as hash tables. The items can be added, retrieved, and removed using the [] operator, and they can be constructed using the make() method and the map keyword.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable Examples and fmt helps in formatting input and output.

  • Step 2 − Use the const keyword to define a new enumeration type called Clothes.

  • Step 3 − Give the integer values 0, 1, and 2 to the enumeration values Saree, Kurti and Shrug accordingly.

  • Step 4 − Make a map called clothes that links the enumeration values of the clothes to their string representations.

  • Step 5 − Utilize the clothes map in the main function to find for the enumeration value of a specific cloth using its string representation.

  • Step 6 − Print the enumeration value on the console using fmt.Printlln() function where ln means new line.

Example

package main
import "fmt"

type Clothes int  //create type clothes

const (
   Saree Clothes = iota
   Kurti
   Shrug
)

var clothes = map[string]Clothes{   //create a map to link enumeration values with string representation
   "saree": Saree,
   "kurti": Kurti,
   "shrug": Shrug,
}

func main() {
   cloth := clothes["saree"]
   fmt.Println("The enum value of particular cloth is:")
   fmt.Println(cloth)  //print the enum value
}

Output

The enum value of particular cloth is:
0

Method 2: Using User-defined Function

This example defines the name and enum fields of a struct called clothStruct. Elements representing the potential clothes are defined as a clothList array of clothStruct, along with their name and enumeration values. This strategy also offers the option to expand the struct's fields or methods as necessary.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable Examples and fmt helps in formatting input and output.

  • Step 2 − Use the const keyword to create a new Cloth enumeration type.

  • Step 3 − Saree, Kurti and Shrug should be defined as integers and given the values 0, 1, and 2, accordingly.

  • Step 4 − Make a struct type called clothStruct with the fields name and enum.

  • Step 5 − Make a clothList of clothStruct array called clothList that has items representing the potential clothes together with their names and enumeration values.

  • Step 6 − Check if a struct's name field matches the supplied cloth string in the main function when you go over the clothList array.

  • Step 7 − Print the enumeration value and end the loop if the name field is a match.

  • Step 8 − In the event that the input string does not match any of the enumeration values, you should always consider adding error handling.

Example

In this example we will see how to lookup enum by string value using user-defined function

package main
import "fmt"
type Cloth int 

const (
   Saree Cloth = iota    //create cloth enumeration type
   Kurti
   Shrug
)

type clothStruct struct {
   name string
   enum Cloth
}

var clothList = []clothStruct{
   {name: "saree", enum: Saree},
   {name: "kurti", enum: Kurti},
   {name: "shrug", enum: Shrug},
}

func main() {
   cloth := "kurti"
   fmt.Println("The enum value of cloth is:")
   for _, v := range clothList {
      if v.name == cloth {
         fmt.Println(v.enum)   //print the required enum value
         break
      }
   }
}

Output

The enum value of cloth is:
1

Conclusion

We executed the program to lookup enum by string value using two methods. In the first example we used string representation and in the second example we used struct having string and enum field.

Updated on: 20-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements