# Golang - Command Pattern

The "Command Pattern" is one of the design patterns introduced by the "Gang of Four". This pattern is used to encapsulate the request as an object and allows you to parameterize clients with different requests, queue or log requests, and support undoable operations. In this article, we will discuss the implementation of the "Command Pattern" in Golang.

# Introduction

The "Command Pattern" is a behavioral design pattern that allows you to encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. The pattern is used to decouple the sender of a request from its receiver, giving more flexibility to the system.

The "Command Pattern" consists of the following components:

* `Command`: an interface that defines the execution method.
    
* `ConcreteCommand`: a struct that implements the `Command` interface and contains a receiver object.
    
* `Invoker`: a struct that triggers the execution of the command.
    
* `Receiver`: a struct that receives the request and performs the action.
    

# How it works

The "Command Pattern" is a powerful design pattern that can be used to implement a variety of functionalities in Golang. It provides a clean and easy-to-understand separation of concerns between the client, receiver, and invoker.

The `Command` interface defines the `Execute()` method. The `ConcreteCommand` struct implements the `Command` interface and contains a receiver object. The `Invoker` struct triggers the execution of the command, and the `Receiver` struct receives the request and performs the action.

Here is a simple code example that demonstrates the "Command Pattern" in Golang:

```go
type Command interface {
    Execute() string
}

type ConcreteCommand struct {
    receiver Receiver
}

func (cc *ConcreteCommand) Execute() string {
    return cc.receiver.Action()
}

type Invoker struct {
    command Command
}

func (i *Invoker) ExecuteCommand() string {
    return i.command.Execute()
}

type Receiver struct{}

func (r *Receiver) Action() string {
    return "Action Performed"
}

func main() {
    receiver := &Receiver{}
    concreteCommand := &ConcreteCommand{receiver: *receiver}
    invoker := &Invoker{command: concreteCommand}
    result := invoker.ExecuteCommand()
    fmt.Println(result)
}

```

In this code example, we define the `Command` interface which has a single method `Execute()`. We then define the `ConcreteCommand` struct that implements the `Command` interface and contains a `Receiver` object. The `Receiver` struct receives the request and performs the action. The `Invoker` struct triggers the execution of the command.

# Usage in Golang CLI Frameworks

Golang CLI frameworks like `cobra` use the Command Pattern to define and execute the CLI commands. `cobra` provides a simple and easy-to-use interface for creating CLI applications. Here is an example of how to create a CLI command using "cobra":

```go
var cmdSay = &cobra.Command{
    Use: "say",
    Short: "say something",
    Long: `say command will print out the message passed in the argument`,
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("Saying:", strings.Join(args, " "))
    },
}
```

In this example, `cmdSay` is a `ConcreteCommand`, and `cobra.Command` is an `Invoker`. The `Run` function is equivalent to the `Execute` function in the Command Pattern. When the user runs the command `say`, the `Run` function is executed.

# Benefits of using the "Command Pattern"

The "Command Pattern" provides a number of benefits when used in Golang applications. These include:

* Separation of concerns: The "Command Pattern" allows us to separate the receiver of a request from its sender, thereby providing more flexibility to the system.
    
* Parameterization: The pattern allows parameterization of clients with different requests.
    
* Queueing and logging: The pattern allows requests to be queued or logged for later use.
    
* Support for undoable operations: The pattern enables support for undoable operations, which can be used to revert the system to a previous state.
    

# Conclusion

In conclusion, the "Command Pattern" is a powerful design pattern that can be used to implement a variety of functionalities in Golang. It provides a clean and easy-to-understand separation of concerns between the client, receiver, and invoker. Additionally, CLI frameworks like "cobra" provide an excellent way of using the "Command Pattern" to create CLI applications in Golang. By using this pattern, it is possible to create flexible and scalable applications that are easy to maintain and extend.

---

# References

* [**“Design Patterns: Elements of Reusable Object-Oriented Software**](https://amzn.to/3SJenIZ)” by Erich Gamma, John Vlissides, Ralph Johnson, and Richard Helm
    
* [**“spf13/cobra**”](https://github.com/spf13/cobra) on GitHub
