Guest User

Untitled

a guest
Jun 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math"
  6. )
  7.  
  8. const SENIOR int32 = 65
  9.  
  10. type Persona struct {
  11. name string
  12. lastName string
  13. age int32
  14. }
  15.  
  16. // Defining one method for `Persona`
  17. func (p Persona) getFullName() string {
  18. return fmt.Sprintf("%s, %s", p.lastName, p.name)
  19. }
  20.  
  21. func (p Persona) isSenior() bool {
  22. return p.age >= SENIOR
  23. }
  24.  
  25. func (p *Persona) becomeSenior() {
  26. p.age = 65
  27. }
  28.  
  29. func byval(a int, b int) {
  30. a = 2
  31. b = 3
  32. }
  33.  
  34. func byref(a *int, b *int) {
  35. *a = 3
  36. *b = 4
  37. }
  38.  
  39. // Interfaces let you use duck typing like you would in a purely dynamic
  40. // language like Python
  41. type ReadCloser interface {
  42. Read(b []byte) (n int, err int)
  43. Close()
  44. }
  45.  
  46. // The code that calls `ReadAndClose` can pass a value of any type as long as it
  47. // has `Read` and `Close` methods with the right signatures
  48. func ReadAndClose(r ReadCloser, buf []byte) (n int, err int) {
  49. for len(buf) > 0 && err == 0 {
  50. var nr int
  51. nr, err = r.Read(buf)
  52. n += nr
  53. buf = buf[nr:]
  54. }
  55. r.Close()
  56. return
  57. }
  58.  
  59. // Interfaces aren't restricted to static checking. You can check dynamically
  60. // whether a particular interface value has an additional method.
  61. type Stringer interface {
  62. String() string
  63. }
  64.  
  65. func ToString(any interface{}) string {
  66. return "???"
  67. }
  68.  
  69. type geometry interface {
  70. area() float64
  71. perim() float64
  72. }
  73.  
  74. type rect struct {
  75. width, height float64
  76. }
  77.  
  78. type circle struct {
  79. radius float64
  80. }
  81.  
  82. // In order to implement an interface in Go, we just need to implement all the
  83. // methods in the interface
  84. func (r rect) area() float64 {
  85. return r.width * r.height
  86. }
  87.  
  88. func (r rect) perim() float64 {
  89. return 2*r.width + 2*r.height
  90. }
  91.  
  92. func (c circle) area() float64 {
  93. return math.Pi * c.radius * c.radius
  94. }
  95.  
  96. func (c circle) perim() float64 {
  97. return 2 * math.Pi * c.radius
  98. }
  99.  
  100. // If a variable has an interface type, then we can call methods that are in
  101. // the named interface
  102. func measure(g geometry) {
  103. fmt.Println(g)
  104. fmt.Println(g.area())
  105. fmt.Println(g.perim())
  106. }
  107.  
  108. // Variadic functions
  109. func printNumbers(nums ...int) {
  110. fmt.Println(nums)
  111. }
  112.  
  113. // Main entry point
  114. func main() {
  115. a := 1
  116. b := 2
  117.  
  118. fmt.Println(a)
  119. fmt.Println(b)
  120.  
  121. byref(&a, &b)
  122. fmt.Println(a)
  123. fmt.Println(b)
  124.  
  125. // static array
  126. var arr [7]int
  127. arr[4] = 5
  128. fmt.Println(arr)
  129.  
  130. // slice: A slice is a segment of an array. Like arrays slices are indexable
  131. // and have a length. Unlike arrays this length is allowed to change.
  132. // Slices can be created with the built-in make function; this is how you
  133. // create dynamically-sized arrays
  134. ss := make([]int, 10)
  135. ss[8] = 2
  136. fmt.Println(ss)
  137.  
  138. // Using a new dynamic size array. No need to use `make`
  139. var tt []int
  140. tt = append(tt, 3)
  141. fmt.Println(tt)
  142.  
  143. var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
  144. for i, v := range pow {
  145. fmt.Println(i)
  146. fmt.Println(v)
  147. }
  148.  
  149. for _, v := range pow {
  150. fmt.Println(v)
  151. }
  152.  
  153. // hashtable, dictionary => maps
  154. testMap := make(map[string]int)
  155.  
  156. testMap["hello"] = 10
  157. testMap["bye"] = 20
  158.  
  159. _, exists := testMap["hello"]
  160. if exists {
  161. fmt.Println("hello exists in hash")
  162. }
  163.  
  164. for k, v := range testMap {
  165. fmt.Printf("%v: %v\n", k, v)
  166. result := fmt.Sprintf("%s => %d", k, v)
  167. fmt.Println(result)
  168. }
  169.  
  170. // Function literals are closures
  171. xBig := func() bool {
  172. // `a` was defined before so closure can use it here
  173. return a > -1
  174. }
  175. fmt.Println("xBig: ", xBig())
  176.  
  177. // Structs
  178. john := Persona{"John", "Smith", 27}
  179. fmt.Println(john)
  180. fmt.Println(john.getFullName())
  181. fmt.Println(john.isSenior())
  182. john.becomeSenior()
  183. fmt.Println(john.isSenior())
  184.  
  185. // Interfaces
  186. rr := rect{width: 3, height: 2}
  187. cc := circle{radius: 5}
  188.  
  189. measure(rr)
  190. measure(cc)
  191.  
  192. // Calling to variadic func
  193. printNumbers(1, 2)
  194. printNumbers(1, 2, 3)
  195. }
Add Comment
Please, Sign In to add comment