-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path06_interface_implementation.go
50 lines (42 loc) · 1.23 KB
/
06_interface_implementation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Čtvrtá část
// Rozhraní, metody, gorutiny a kanály v programovacím jazyku Go
// https://www.root.cz/clanky/rozhrani-metody-gorutiny-a-kanaly-v-programovacim-jazyku-go/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů ze čtvrté části:
// https://github.com/tisnik/go-root/blob/master/article_04/README.md
//
// Demonstrační příklad číslo 6:
// Implementace jednoduchého rozhraní nazvaného OpenShape.
//
// Dokumentace ve stylu "literate programming":
// https://tisnik.github.io/go-root/article_04/06_interface_implementation.html
package main
import (
"fmt"
"math"
)
// OpenShape je uživatelsky definovaná datová struktura
// představující otevřené tvary (úsečka, oblouk, křivka)
type OpenShape interface {
length() float64
}
// Line je datová struktura, ke které mohou být přidány metody
type Line struct {
x1, y1 float64
x2, y2 float64
}
func length(shape OpenShape) float64 {
return shape.length()
}
func main() {
line1 := Line{x1: 0, y1: 0, x2: 100, y2: 100}
fmt.Println(line1)
lineLength := length(line1)
fmt.Println(lineLength)
}