mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-17 10:41:17 +00:00
first
This commit is contained in:
27
course/5-interfaces/exercises/4a-quiz/readme.md
Normal file
27
course/5-interfaces/exercises/4a-quiz/readme.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# Interfaces Quiz
|
||||
|
||||
Remember, interfaces are collections of method signatures. A type "implements" an interface if it has all of the methods of the given interface defined on it.
|
||||
|
||||
```go
|
||||
type shape interface {
|
||||
area() float64
|
||||
}
|
||||
```
|
||||
|
||||
If a type in your code implements an `area` method, with the same signature (e.g. accepts nothing and returns a `float64`), then that object is said to *implement* the `shape` interface.
|
||||
|
||||
```go
|
||||
type circle struct{
|
||||
radius int
|
||||
}
|
||||
|
||||
func (c *circle) area() float64 {
|
||||
return 3.14 * c.radius * c.radius
|
||||
}
|
||||
```
|
||||
|
||||
This is *different from most other languages*, where you have to *explicitly* assign an interface type to an object, like with Java:
|
||||
|
||||
```java
|
||||
class Circle implements Shape
|
||||
```
|
||||
Reference in New Issue
Block a user