Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. type IPAddr [4]byte
  6.  
  7. // TODO: Add a "String() string" method to IPAddr.
  8. func (ip IPAddr) String() string {
  9. rs := ""
  10.  
  11. for k, v := range ip {
  12. if k == 0 {
  13. rs += fmt.Sprintf("%v", v)
  14. continue
  15. }
  16.  
  17. rs += fmt.Sprintf(".%v", v)
  18. }
  19.  
  20. return rs
  21. }
  22.  
  23. func main() {
  24. hosts := map[string]IPAddr{
  25. "loopback": {127, 0, 0, 1},
  26. "googleDNS": {8, 8, 8, 8},
  27. }
  28. for name, ip := range hosts {
  29. fmt.Printf("%v: %vn", name, ip)
  30. }
  31. }
  32.  
  33. package main
  34.  
  35. import "fmt"
  36.  
  37. type IPAddr [4]byte
  38.  
  39. // TODO: Add a "String() string" method to IPAddr.
  40. func (ip IPAddr) String() string {
  41. return fmt.Sprintf("%v.%v.%v.%v", ip[0], ip[1], ip[2], ip[3])
  42. }
  43.  
  44. func main() {
  45. hosts := map[string]IPAddr{
  46. "loopback": {127, 0, 0, 1},
  47. "googleDNS": {8, 8, 8, 8},
  48. }
  49. for name, ip := range hosts {
  50. fmt.Printf("%v: %vn", name, ip)
  51. }
  52. }
  53.  
  54. package main
  55.  
  56. import (
  57. "fmt"
  58. "net"
  59. )
  60.  
  61. type IPAddr [4]byte
  62.  
  63. func (ip IPAddr) String() string {
  64. return net.IP(ip[:]).String()
  65. }
  66.  
  67. func main() {
  68. hosts := map[string]IPAddr{
  69. "loopback": {127, 0, 0, 1},
  70. "googleDNS": {8, 8, 8, 8},
  71. }
  72. for name, ip := range hosts {
  73. fmt.Printf("%v: %vn", name, ip)
  74. }
  75. }
  76.  
  77. loopback: 127.0.0.1
  78. googleDNS: 8.8.8.8
  79.  
  80. package main
  81.  
  82. import (
  83. "fmt"
  84. "strconv"
  85. )
  86.  
  87. type IPAddr [4]byte
  88.  
  89. func (ip IPAddr) String() string {
  90. s := make([]byte, 0, (1+3)*len(IPAddr{}))
  91. for i, b := range ip {
  92. if i > 0 {
  93. s = append(s, ',')
  94. }
  95. s = strconv.AppendInt(s, int64(b), 10)
  96. }
  97. return string(s)
  98. }
  99.  
  100. func main() {
  101. hosts := map[string]IPAddr{
  102. "loopback": {127, 0, 0, 1},
  103. "googleDNS": {8, 8, 8, 8},
  104. }
  105. for name, ip := range hosts {
  106. fmt.Printf("%v: %vn", name, ip)
  107. }
  108. }
  109.  
  110. loopback: 127,0,0,1
  111. googleDNS: 8,8,8,8
  112.  
  113. package main
  114.  
  115. import (
  116. "fmt"
  117. "testing"
  118. )
  119.  
  120. type IPAddr [4]byte
  121.  
  122. func (ip IPAddr) String() string {
  123. rs := ""
  124. for k, v := range ip {
  125. if k == 0 {
  126. rs += fmt.Sprintf("%v", v)
  127. continue
  128. }
  129. rs += fmt.Sprintf(".%v", v)
  130. }
  131. return rs
  132. }
  133.  
  134. func BenchmarkString1(b *testing.B) {
  135. hosts := map[string]IPAddr{
  136. "loopback": {127, 0, 0, 1},
  137. "googleDNS": {8, 8, 8, 8},
  138. }
  139. for N := 0; N < b.N; N++ {
  140. for name, ip := range hosts {
  141. fmt.Sprintf("%v: %vn", name, ip)
  142. }
  143. }
  144. }
  145.  
  146. BenchmarkString1-8 841730 1412 ns/op 176 B/op 19 allocs/op
  147. BenchmarkString2-8 1228399 931 ns/op 128 B/op 8 allocs/op
  148. BenchmarkString3-8 1984806 574 ns/op 128 B/op 8 allocs/op
  149. BenchmarkString4-8 1765022 626 ns/op 128 B/op 8 allocs/op
  150.  
  151. func (ip IPAddr) String() string {
  152. return fmt.Sprintf("%v.%v.%v.%v", ip[0], ip[1], ip[2], ip[3])
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement