Initialising of types — Enumerations

In Go, enumerations can be implemented using constants and iota. While Go doesn't have a built-in enum type like some other languages, you can use a combination of constants and custom types to achieve similar functionality. Let's go through how to define, initialize, and use enumerations in Go.

Example: Defining and Using Enumerations

Step 1: Defining the Enumeration

You can define an enumeration by creating a new type and then declaring constants of that type, using iota to auto-increment the values.

package main

import (
	"fmt"
)

// Weekday is a custom type for the enumeration
type Weekday int

// Define enumeration values using iota
const (
	Sunday Weekday = iota
	Monday
	Tuesday
	Wednesday
	Thursday
	Friday
	Saturday
)

// String method to get the name of the weekday
func (w Weekday) String() string {
	names := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
	if w < Sunday || w > Saturday {
		return "Unknown"
	}
	return names[w]
}

func main() {
	// Initializing a variable with the enumeration type
	var day Weekday = Wednesday

	// Printing the value and its string representation
	fmt.Println(day)        // Output: 3
	fmt.Println(day.String()) // Output: Wednesday
}

Explanation

  1. Defining the Enumeration Type:

    type Weekday int
    
    

    We define a new type Weekday which is an alias for int.

  2. Declaring Enumeration Values:

    const (
        Sunday Weekday = iota
        Monday
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
    )
    
    

    We use iota to auto-increment the constants, starting from 0 for Sunday up to 6 for Saturday.

  3. Adding a String Method:

    func (w Weekday) String() string {
        names := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
        if w < Sunday || w > Saturday {
            return "Unknown"
        }
        return names[w]
    }
    
    

    We add a String method to the Weekday type to get the name of the weekday. This method returns the string representation of the enumeration value.

  4. Using the Enumeration:

    var day Weekday = Wednesday
    fmt.Println(day)        // Output: 3
    fmt.Println(day.String()) // Output: Wednesday
    
    

    We initialize a variable day with the enumeration value Wednesday and print both its integer value and string representation.

Example: Enumeration with Switch Statements

Let's extend the example to use enumerations with switch statements.

package main

import (
	"fmt"
)

// Weekday is a custom type for the enumeration
type Weekday int

// Define enumeration values using iota
const (
	Sunday Weekday = iota
	Monday
	Tuesday
	Wednesday
	Thursday
	Friday
	Saturday
)

// String method to get the name of the weekday
func (w Weekday) String() string {
	names := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
	if w < Sunday || w > Saturday {
		return "Unknown"
	}
	return names[w]
}

// IsWeekend checks if the given day is a weekend
func IsWeekend(day Weekday) bool {
	switch day {
	case Saturday, Sunday:
		return true
	default:
		return false
	}
}

func main() {
	// Initializing a variable with the enumeration type
	var day Weekday = Wednesday

	// Using the enumeration in a switch statement
	switch day {
	case Monday, Tuesday, Wednesday, Thursday, Friday:
		fmt.Println(day.String(), "is a weekday.")
	case Saturday, Sunday:
		fmt.Println(day.String(), "is a weekend.")
	default:
		fmt.Println("Invalid day")
	}

	// Checking if the day is a weekend
	if IsWeekend(day) {
		fmt.Println(day.String(), "is a weekend.")
	} else {
		fmt.Println(day.String(), "is a weekday.")
	}
}

Explanation

  1. Using Enumerations in Switch Statements:

    switch day {
    case Monday, Tuesday, Wednesday, Thursday, Friday:
        fmt.Println(day.String(), "is a weekday.")
    case Saturday, Sunday:
        fmt.Println(day.String(), "is a weekend.")
    default:
        fmt.Println("Invalid day")
    }
    
    

    We use a switch statement to check the value of day and print whether it is a weekday or weekend.

  2. Defining a Function to Check Weekends:

    func IsWeekend(day Weekday) bool {
        switch day {
        case Saturday, Sunday:
            return true
        default:
            return false
        }
    }
    
    

    We define an IsWeekend function that returns true if the day is Saturday or Sunday, and false otherwise.

Conclusion

By defining custom types and constants, you can create enumerations in Go. This allows you to use meaningful names for your constants and ensures type safety. The examples above demonstrate how to define, initialize, and use enumerations, as well as how to implement methods and use them in switch statements. This approach adheres to Go's idiomatic use of constants and types while providing similar functionality to enumerations in other languages.

Initialising of types — structures — Without any constructor