-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path02_methods_with_parameters.go
56 lines (48 loc) · 1.34 KB
/
02_methods_with_parameters.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
51
52
53
54
55
56
// 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 2:
// Metoda bez parametrů a metoda s parametry.
//
// Dokumentace ve stylu "literate programming":
// https://tisnik.github.io/go-root/article_04/02_methods_with_parameters.html
package main
import (
"fmt"
"math"
)
// Line je uživatelsky definovaná datová struktura
type Line struct {
x1, y1 float64
x2, y2 float64
}
// Metoda definovaná pro strukturu Line
func (line Line) length() float64 {
return math.Hypot(line.x1-line.x2, line.y1-line.y2)
}
// Metoda definovaná pro strukturu Line
func (line Line) translate(dx, dy float64) {
fmt.Printf("Translating line %v by %f %f\n", line, dx, dy)
line.x1 += dx
line.y1 += dy
line.x2 += dx
line.y2 += dy
}
func main() {
line1 := Line{x1: 0, y1: 0, x2: 100, y2: 100}
fmt.Println(line1)
line1.translate(5, 5)
fmt.Println(line1)
lineLength := line1.length()
fmt.Println(lineLength)
}