-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.go
More file actions
51 lines (41 loc) · 1.46 KB
/
Copy pathvalue.go
File metadata and controls
51 lines (41 loc) · 1.46 KB
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
package mygostructs
import "fmt"
// Item the interface used as main item in the different structs. Any data that you
// want store in a struct must be implements this interface. You can find an example if you revise
// the code of the IntItem struct.
type Item interface {
// Less checks if the item is more less than the item of the parameter.
Less(Item) bool
// Eq checks if the item is Eq to the item of the parameter.
Eq(Item) bool
// String transforms the item to string.
String() string
}
// IntItem structs is an implementation of the Item interface specific for storing int numbers.
type IntItem struct {
value int // number stored
}
// Less checks if the iit item is more less than the item of the parameter.
// The function also returns false if it paramater isn't type IntItem.
func (iit IntItem) Less(it Item) bool {
iitp, valid := it.(IntItem)
return valid && iit.value < iitp.value
}
// Eq checks if the iit item is equal to the item of the paramater.
// The function also returns false if it paramater isn't type IntItem.
func (iit IntItem) Eq(it Item) bool {
iitp, valid := it.(IntItem)
return valid && iit.value == iitp.value
}
// String returns the number as string.
func (iit IntItem) String() string {
return fmt.Sprintf("%d", iit.value)
}
// Value returns the number stored in iit.
func (iit IntItem) Value() int {
return iit.value
}
// It creates an IntItem object with the number of the param.
func It(num int) IntItem {
return IntItem{num}
}