push all exercices
This commit is contained in:
38
ch8/2/ch8_2.go
Normal file
38
ch8/2/ch8_2.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Printable interface {
|
||||
Number | NumberFloat
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
type Number struct {
|
||||
val int
|
||||
}
|
||||
|
||||
func (num Number) String() string {
|
||||
return string(strconv.Itoa(num.val))
|
||||
}
|
||||
|
||||
type NumberFloat struct {
|
||||
val float64
|
||||
}
|
||||
|
||||
func (num NumberFloat) String() string {
|
||||
return string(strconv.Itoa(int(num.val)))
|
||||
}
|
||||
|
||||
func Print[T Printable](num T) {
|
||||
fmt.Println(num)
|
||||
}
|
||||
|
||||
func main() {
|
||||
firstTest := Number{val: 5}
|
||||
secondTest := NumberFloat{val: 7.2}
|
||||
Print(firstTest)
|
||||
Print(secondTest)
|
||||
}
|
||||
Reference in New Issue
Block a user