MED Message Formatter

As Textio evolves, the team has decided to introduce a new feature for custom message formats. Depending on the user's preferences, messages can be sent in different formats, such as plain text, markdown, code, or even encrypted text. To efficiently manage this, you'll implement a system using interfaces.

Assignment

1

Implement the formatter interface

Create an interface named formatter with a method:

  • format() string β€” returns the formatted string.

2

Define structs that satisfy formatter

Create the following structs, each having a message string field, and implement format() for each:

  • plainText β€” returns the message as is.

  • bold β€” wraps the message in two asterisks to simulate bold (e.g., **message**).

  • code β€” wraps the message in a single backtick to simulate inline code (e.g., `message`).

Solution

solution.go
package main

type formatter interface {
	format() string
}

type plainText struct {
	message string
}

func (pt plainText) format() string {
	return pt.message
}

type bold struct {
	message string
}

func (b bold) format() string {
	return "**" + b.message + "**"
}

type code struct {
	message string
}

func (c code) format() string {
	return "`" + c.message + "`"
}

// Don't Touch below this line

func sendMessage(format formatter) string {
	return format.format()
}