-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-second.go
92 lines (81 loc) · 1.6 KB
/
my-second.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"hash/crc32"
"io"
"math"
"os"
)
type Shape interface {
area() float64
}
type Circle struct {
x, y, r float64
}
type Rectangle struct {
x1, y1, x2, y2 float64
}
type MultiShape struct {
shapes []Shape
}
func (c *Circle) area() float64 {
return math.Pi * c.r * c.r
}
func (r *Rectangle) area() float64 {
l := distance(r.x1, r.y1, r.x1, r.y2)
w := distance(r.x1, r.y1, r.x2, r.y1)
return l * w
}
func totalArea(shapes ...Shape) float64 {
var area float64
for _, s := range shapes {
area += s.area()
}
return area
}
type Persona struct {
Name string
Age int
}
type ByName []Persona
func (ps ByName) Len() int {
return len(ps)
}
func (ps ByName) Less(i, j int) bool {
return ps[i].Name < ps[j].Name
}
func (ps ByName) Swap(i, j int) {
ps[i], ps[j] = ps[j], ps[i]
}
type ByAge []Persona
func (this ByAge) Len() int {
return len(this)
}
func (this ByAge) Less(i, j int) bool {
return this[i].Age < this[j].Age
}
func (this ByAge) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}
func getHash(filename string) (uint32, error) { // open the file
f, err := os.Open(filename)
if err != nil {
return 0, err
}
// remember to always close opened files
defer f.Close() // create a hasher
h := crc32.NewIEEE()
// copy the file into the hasher
// - copy takes (dst, src) and returns (bytesWritten, error)
_, err = io.Copy(h, f)
// we don't care about how many bytes were written, but we do want to // handle the error
if err != nil {
return 0, err
}
return h.Sum32(), nil
}
func routinefn(from string) {
for i := 0; i < 3; i++ {
fmt.Println(from, ":", i)
}
}