golang enum type

package main

import (

type Status int

const (
    Pending Status = iota

func (s Status) String() string {
    statusStrings := [...]string{"Pending", "InProgress", "Completed", "Failed"}
    if s < Pending || s > Failed {
        return "Unknown"
    return statusStrings[s]

func main() {
    var taskStatus Status = Completed
    fmt.Println("Task status:", taskStatus.String())


  1. Package Declaration: The main package is declared, which is the entry point of execution for Go programs.

  2. Imports: The fmt package is imported for handling input/output operations.

  3. Enum Definition: A new type Status is declared using the int underlying type.

  4. Constants: The const block defines different statuses (Pending, InProgress, Completed, Failed) using the iota enumerator to auto-increment their values (0, 1, 2, 3).

  5. Stringer Method: A String() method is defined for the Status type to return a string representation of the status using an array of corresponding status strings.

  6. Main Function: Inside the main function, a variable taskStatus of type Status is declared and initialized with the Completed status.

  7. Println: Finally, the string representation of the taskStatus is printed to the console using fmt.Println.