interface_task.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. batTest()
  8. // ExampleStringer()
  9. // foo1()
  10. /*
  11. value1, value2, operation := readTask()
  12. switch v1 := value1.(type) {
  13. case float64:
  14. break
  15. default:
  16. fmt.Printf("value=%v: %T\n", v1, v1)
  17. return
  18. }
  19. switch v2 := value2.(type) {
  20. case float64:
  21. break
  22. default:
  23. fmt.Printf("value=%v: %T\n", v2, v2)
  24. return
  25. }
  26. switch op := operation.(type) {
  27. case string:
  28. if op == "+" {
  29. fmt.Printf("%.4f\n", value1.(float64)+value2.(float64))
  30. } else if op == "-" {
  31. fmt.Printf("%.4f\n", value1.(float64)-value2.(float64))
  32. } else if op == "*" {
  33. fmt.Printf("%.4f\n", value1.(float64)*value2.(float64))
  34. } else if op == "/" {
  35. fmt.Printf("%.4f\n", value1.(float64)/value2.(float64))
  36. }
  37. default:
  38. fmt.Printf("operation=%v: %T\n", op, op)
  39. return
  40. }
  41. // fmt.Println("value 1 - OK")
  42. // fmt.Println(value1, value2, operation)
  43. */
  44. }
  45. func foo1() {
  46. // vi1, vi2, operation := readTask()
  47. // vi := [2]interface{}{vi1, vi2}
  48. var v1, v2 float64
  49. vf := [2]*float64{&v1, &v2}
  50. fmt.Println(vf)
  51. fmt.Printf("%v %T", vf, vf)
  52. }
  53. func readTask() (v1, v2, op interface{}) {
  54. v1 = 12.36
  55. v2 = 36.24
  56. op = "+"
  57. return
  58. }
  59. type Animal struct {
  60. Name string
  61. Age uint
  62. }
  63. func (a Animal) String() string {
  64. return fmt.Sprintf("%v (%d)", a.Name, a.Age)
  65. }
  66. func ExampleStringer() {
  67. a := Animal{
  68. Name: "Gopher",
  69. Age: 2,
  70. }
  71. fmt.Println(a)
  72. }
  73. type Battery struct {
  74. capacity int
  75. }
  76. func (b Battery) String() string {
  77. var ret string = "["
  78. for i := 0; i < (10 - b.capacity); i++ {
  79. ret += " "
  80. }
  81. for i := 0; i < b.capacity; i++ {
  82. ret += "X"
  83. }
  84. ret += "]"
  85. return ret
  86. }
  87. func batTest() {
  88. // var batteryForTest Battery
  89. var charge string
  90. batteryForTest := Battery{}
  91. fmt.Scan(&charge)
  92. batteryForTest.capacity = strings.Count(charge, "1")
  93. fmt.Println(batteryForTest)
  94. }