|
| 1 | +# Airport Robot |
| 2 | + |
| 3 | +Welcome to Airport Robot on Exercism's Go Track. |
| 4 | +If you need help running the tests or submitting your code, check out `HELP.md`. |
| 5 | +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +## Interface as a set of methods |
| 10 | + |
| 11 | +In its simplest form, an **interface type** is a set of method signatures. |
| 12 | +Here is an example of an interface definition that includes two methods `Add` and `Value`: |
| 13 | + |
| 14 | +```go |
| 15 | +type Counter interface { |
| 16 | + Add(increment int) |
| 17 | + Value() int |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +The parameter names like `increment` can be omitted from the interface definition but they often increase readability. |
| 22 | + |
| 23 | +Interface names in Go do not contain the word `Interface` or `I`. |
| 24 | +Instead, they often end with `er`, e.g. `Reader`, `Stringer`. |
| 25 | + |
| 26 | +## Implementing an interface |
| 27 | + |
| 28 | +Any type that defines the methods of the interface automatically implicitly "implements" the interface. |
| 29 | +There is no `implements` keyword in Go. |
| 30 | + |
| 31 | +The following type implements the `Counter` interface we saw above. |
| 32 | + |
| 33 | +```go |
| 34 | +type Stats struct { |
| 35 | + value int |
| 36 | + // ... |
| 37 | +} |
| 38 | + |
| 39 | +func (s Stats) Add(v int) { |
| 40 | + s.value += v |
| 41 | +} |
| 42 | + |
| 43 | +func (s Stats) Value() int { |
| 44 | + return s.value |
| 45 | +} |
| 46 | + |
| 47 | +func (s Stats) SomeOtherMethod() { |
| 48 | + // The type can have additional methods not mentioned in the interface. |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +For implementing the interface, it does not matter whether the method has a value or pointer receiver. |
| 53 | +(Revisit the [methods concepts][concept-methods] if you are unsure about those.) |
| 54 | + |
| 55 | +> A value of interface type can hold any value that implements those methods. [^1] |
| 56 | +
|
| 57 | +That means `Stats` can now be used in all the places that expect the `Counter` interface. |
| 58 | + |
| 59 | +```go |
| 60 | +func SetUpAnalytics(counter Counter) { |
| 61 | + // ... |
| 62 | +} |
| 63 | + |
| 64 | +stats := Stats{} |
| 65 | +SetUpAnalytics(stats) |
| 66 | +// works because Stats implements Counter |
| 67 | +``` |
| 68 | + |
| 69 | +Because interfaces are implemented implicitly, a type can easily implement multiple interfaces. |
| 70 | +It only needs to have all the necessary methods defined. |
| 71 | + |
| 72 | +## Empty interface |
| 73 | + |
| 74 | +There is one very special interface type in Go, the **empty interface** type that contains zero methods. |
| 75 | +The empty interface is written like this: `interface{}`. |
| 76 | +In Go 1.18 or higher, `any` can be used as well. It was defined as an alias. |
| 77 | + |
| 78 | +Since the empty interface has no methods, every type implements it implicitly. |
| 79 | +This is helpful for defining a function that can generically accept any value. |
| 80 | +In that case, the function parameter uses the empty interface type. |
| 81 | + |
| 82 | +[concept-methods]: /tracks/go/concepts/methods |
| 83 | + |
| 84 | +## Instructions |
| 85 | + |
| 86 | +The new airport in Berlin hired developers for their robots lab and you are starting your job there. |
| 87 | +They have clunky, somewhat humanoid-looking robots that they are trying to use to improve customer service. |
| 88 | + |
| 89 | +Your first task on the job is to write a program so that the robot can greet people in their native language after they scanned their passports at the self-check-in counter. |
| 90 | + |
| 91 | +The robot is proud of its abilities so it will always say which language it can speak first and then greet the person. |
| 92 | +For example, if someone scans a German passport the robot would say: |
| 93 | + |
| 94 | +```txt |
| 95 | +I can speak German: Hallo Dietrich! |
| 96 | +``` |
| 97 | + |
| 98 | +## 1. Create the abstract greeting functionality |
| 99 | + |
| 100 | +You will not write the code for the different languages yourself so you need to structure your code for the robot so that other developers can easily add more languages later. |
| 101 | + |
| 102 | +As a first step, define an interface `Greeter` with two methods. |
| 103 | + |
| 104 | +- `LanguageName` which returns the name of the language (a `string`) that the robot is supposed to greet the visitor in. |
| 105 | +- `Greet` which accepts a visitor's name (a `string`) and returns a `string` with the greeting message in a specific language. |
| 106 | + |
| 107 | +Next, implement a function `SayHello` that accepts the name of the visitor and anything that implements the `Greeter` interface as arguments and returns the desired greeting string. |
| 108 | +For example, imagine a German `Greeter` implementation for which `LanguageName` returns `"German"` and `Greet` returns `"Hallo {name}!"`: |
| 109 | + |
| 110 | +```go |
| 111 | +SayHello("Dietrich", germanGreeter) |
| 112 | +// => "I can speak German: Hallo Dietrich!" |
| 113 | +``` |
| 114 | + |
| 115 | +## 2. Implement Italian |
| 116 | + |
| 117 | +Now your job is to make the robot work for people that scan Italian passports. |
| 118 | + |
| 119 | +For that, create a struct `Italian` and implement the two methods that are needed for the struct to fulfill the `Greeter` interface you set up in task 1. |
| 120 | +You can greet someone in Italian with `"Ciao {name}!"`. |
| 121 | + |
| 122 | +## 3. Implement Portuguese |
| 123 | + |
| 124 | +Before you call it a day, you are also supposed to finish the functionality to greet people in Portuguese. |
| 125 | + |
| 126 | +For that, create a struct `Portuguese` and implement the two methods that are needed for the struct to fulfill the `Greeter` interface here as well. |
| 127 | +You can greet someone in Portuguese with `"Olá {name}!"`. |
| 128 | + |
| 129 | +## Source |
| 130 | + |
| 131 | +### Created by |
| 132 | + |
| 133 | +- @junedev |
0 commit comments