Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/pkg/errors"
  8. "strings"
  9. )
  10.  
  11. // 1. use interfaces to interact between modules
  12.  
  13. type Person interface {
  14. GetName() string
  15. GetAge() int
  16. }
  17.  
  18. // implementation
  19. type Man struct {
  20. name string
  21. age int
  22. }
  23.  
  24. func (m Man) GetName() string {
  25. return m.name
  26. }
  27.  
  28. func (m Man) GetAge() int {
  29. return m.age
  30. }
  31.  
  32. func printPerson(person Person) {
  33. fmt.Printf("Name %s. Age %d", person.GetName(), person.GetAge())
  34. }
  35.  
  36. // 2. Do not ignore errors
  37.  
  38. func test() error {
  39. // Good
  40. if err := os.Chown("/tmp/test", 2, 2); err != nil {
  41. return err
  42. }
  43. return nil
  44. // Bad
  45. // os.Chown("/tmp/test", 2, 2)
  46. }
  47.  
  48. // 3. letter case struct fields are not available from other modules struct functions and cat not be serializable via Json
  49.  
  50. func test1() {
  51. p := Man{}
  52.  
  53. // Not available from foreign modules
  54. // p.name
  55.  
  56. // Use methods instead
  57. // p.GetName()
  58.  
  59. // Or fields starting with Upper case(BAD)
  60. // p.Name
  61.  
  62. // Create special structure with upper case fields for marshalling and unmarshalling
  63.  
  64. fmt.Printf("Data: %v", p)
  65. }
  66.  
  67. // 4. for cycle
  68.  
  69. func main1() {
  70. g := []int{1, 2, 3, 4, 5, 6, 7}
  71.  
  72. // Bad
  73. for _, value := range g {
  74. go func() {
  75. fmt.Printf("Value is: %d \n", value)
  76. }()
  77. }
  78. time.Sleep(time.Duration(2) * time.Second)
  79.  
  80. // Good
  81. for _, value := range g {
  82. // pass cycle value as function parameter!
  83. go func(param int) {
  84. fmt.Printf("Value is: %d \n", param)
  85. }(value)
  86. }
  87. time.Sleep(time.Duration(2) * time.Second)
  88. }
  89.  
  90. // 5. if clause
  91. func test51() (string, error) {
  92. return "lol", nil
  93. }
  94.  
  95. func main() {
  96. var res string
  97.  
  98. // pay attention to so-called variables inside if-clause
  99. if res, _ := test51(); res != "" {
  100. // res variable scope
  101. fmt.Printf("Res: %s \n", res)
  102. }
  103.  
  104. fmt.Printf("Res: %s \n", res)
  105.  
  106. // do not allow golang to make decision about partially definition
  107. res, err := test51()
  108. _ = err
  109.  
  110. // res variable scope
  111. fmt.Printf("Res: %s \n", res)
  112. }
  113.  
  114. // 6. errors are not strings
  115.  
  116. func test6() {
  117. err1 := errors.New("test error")
  118. err2 := errors.New("test1 error")
  119.  
  120. if err1 == err2 {
  121. fmt.Printf("wtf?")
  122. }
  123.  
  124. fmt.Printf("Err1: %s. Err2: %s", err1, err2)
  125.  
  126. // please, no
  127. if strings.Contains(err1.Error(), "test ") {
  128.  
  129. }
  130.  
  131. // more advanced error handling - later
  132. }
  133.  
  134. // Course: Effective go
  135. // https://talks.golang.org/2012/10things.slide#1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement