25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

204 satır
7.8 KiB

  1. // Copyright 2018 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "log"
  18. "os"
  19. "time"
  20. "unsafe"
  21. )
  22. type FooInterface interface {
  23. Bar()
  24. }
  25. type FooStruct struct {
  26. a int
  27. b string
  28. }
  29. func (f *FooStruct) Bar() {}
  30. type myInt int16
  31. var (
  32. Z_bool_false bool = false
  33. Z_bool_true bool = true
  34. Z_int int = -21
  35. Z_int8 int8 = -121
  36. Z_int16 int16 = -32321
  37. Z_int32 int32 = -1987654321
  38. Z_int64 int64 = -9012345678987654321
  39. Z_int_typedef myInt = 88
  40. Z_uint uint = 21
  41. Z_uint8 uint8 = 231
  42. Z_uint16 uint16 = 54321
  43. Z_uint32 uint32 = 3217654321
  44. Z_uint64 uint64 = 12345678900987654321
  45. Z_uintptr uintptr = 21
  46. Z_float32 float32 = 1.54321
  47. Z_float64 float64 = 1.987654321
  48. Z_complex64 complex64 = 1.54321 + 2.54321i
  49. Z_complex128 complex128 = 1.987654321 - 2.987654321i
  50. Z_array [5]int8 = [5]int8{-121, 121, 3, 2, 1}
  51. Z_array_empty [0]int8 = [0]int8{}
  52. Z_array_of_empties [2]struct{} = [2]struct{}{{}, {}}
  53. Z_channel chan int16 = make(chan int16)
  54. Z_channel_2 chan int16 = make(chan int16)
  55. Z_channel_buffered chan int16 = make(chan int16, 10)
  56. Z_channel_nil chan int16
  57. Z_func_bar = (*FooStruct).Bar
  58. Z_func_int8_r_int8 = func(x int8) int8 { return x + 1 }
  59. Z_func_int8_r_pint8 = func(x int8) *int8 { y := x + 1; return &y }
  60. Z_func_nil func(x int8) int8 = nil
  61. Z_interface FooInterface = &Z_struct
  62. Z_interface_typed_nil FooInterface = Z_pointer_nil
  63. Z_interface_nil FooInterface
  64. Z_map map[int8]float32 = map[int8]float32{-21: 3.54321}
  65. Z_map_2 map[int16]int8 = map[int16]int8{1024: 1}
  66. Z_map_3 map[int16]int8 = map[int16]int8{1024: 1, 512: -1}
  67. Z_map_empty map[int8]float32 = map[int8]float32{}
  68. Z_map_nil map[int8]float32
  69. Z_pointer *FooStruct = &Z_struct
  70. Z_pointer_nil *FooStruct
  71. Z_slice []byte = []byte{'s', 'l', 'i', 'c', 'e'}
  72. Z_slice_2 []int8 = Z_array[0:2]
  73. Z_slice_nil []byte
  74. Z_string string = "I'm a string"
  75. Z_struct FooStruct = FooStruct{a: 21, b: "hi"}
  76. Z_unsafe_pointer unsafe.Pointer = unsafe.Pointer(&Z_uint)
  77. Z_unsafe_pointer_nil unsafe.Pointer
  78. )
  79. func foo(x int16, y float32) {
  80. var (
  81. local_array [5]int8 = [5]int8{-121, 121, 3, 2, 1}
  82. local_bool_false bool = false
  83. local_bool_true bool = true
  84. local_channel chan int16 = Z_channel
  85. local_channel_buffered chan int16 = Z_channel_buffered
  86. local_channel_nil chan int16
  87. local_complex128 complex128 = 1.987654321 - 2.987654321i
  88. local_complex64 complex64 = 1.54321 + 2.54321i
  89. local_float32 float32 = 1.54321
  90. local_float64 float64 = 1.987654321
  91. local_func_bar = (*FooStruct).Bar
  92. local_func_int8_r_int8 = func(x int8) int8 { return x + 1 }
  93. local_func_int8_r_pint8 = func(x int8) *int8 { y := x + 1; return &y }
  94. local_func_nil func(x int8) int8 = nil
  95. local_int int = -21
  96. local_int16 int16 = -32321
  97. local_int32 int32 = -1987654321
  98. local_int64 int64 = -9012345678987654321
  99. local_int8 int8 = -121
  100. local_int_typedef myInt = 88
  101. local_interface FooInterface = &Z_struct
  102. local_interface_nil FooInterface
  103. local_interface_typed_nil FooInterface = Z_pointer_nil
  104. local_map map[int8]float32 = map[int8]float32{-21: 3.54321}
  105. local_map_2 map[int16]int8 = map[int16]int8{1024: 1}
  106. local_map_3 map[int16]int8 = map[int16]int8{1024: 1, 512: -1}
  107. local_map_empty map[int8]float32 = map[int8]float32{}
  108. local_map_nil map[int8]float32
  109. local_pointer *FooStruct = &Z_struct
  110. local_pointer_nil *FooStruct
  111. local_slice []byte = []byte{'s', 'l', 'i', 'c', 'e'}
  112. local_slice_2 []int8 = Z_array[0:2]
  113. local_slice_nil []byte
  114. local_string string = "I'm a string"
  115. local_struct FooStruct = FooStruct{a: 21, b: "hi"}
  116. local_uint uint = 21
  117. local_uint16 uint16 = 54321
  118. local_uint32 uint32 = 3217654321
  119. local_uint64 uint64 = 12345678900987654321
  120. local_uint8 uint8 = 231
  121. local_uintptr uintptr = 21
  122. local_unsafe_pointer unsafe.Pointer = unsafe.Pointer(&Z_uint)
  123. local_unsafe_pointer_nil unsafe.Pointer
  124. )
  125. fmt.Println(Z_bool_false, Z_bool_true)
  126. fmt.Println(Z_int, Z_int8, Z_int16, Z_int32, Z_int64, Z_int_typedef)
  127. fmt.Println(Z_uint, Z_uint8, Z_uint16, Z_uint32, Z_uint64, Z_uintptr)
  128. fmt.Println(Z_float32, Z_float64, Z_complex64, Z_complex128)
  129. fmt.Println(Z_array, Z_array_empty, Z_array_of_empties)
  130. fmt.Println(Z_channel, Z_channel_buffered, Z_channel_nil)
  131. fmt.Println(Z_func_bar, Z_func_int8_r_int8, Z_func_int8_r_pint8, Z_func_nil)
  132. fmt.Println(Z_interface, Z_interface_nil, Z_interface_typed_nil)
  133. fmt.Println(Z_map, Z_map_2, Z_map_3, Z_map_empty, Z_map_nil)
  134. fmt.Println(Z_pointer, Z_pointer_nil)
  135. fmt.Println(Z_slice, Z_slice_2, Z_slice_nil)
  136. fmt.Println(Z_string, Z_struct)
  137. fmt.Println(Z_unsafe_pointer, Z_unsafe_pointer_nil)
  138. fmt.Println(local_bool_false, local_bool_true)
  139. fmt.Println(local_int, local_int8, local_int16, local_int32, local_int64, local_int_typedef)
  140. fmt.Println(local_uint, local_uint8, local_uint16, local_uint32, local_uint64, local_uintptr)
  141. fmt.Println(local_float32, local_float64, local_complex64, local_complex128, local_array)
  142. fmt.Println(local_channel, local_channel_buffered, local_channel_nil)
  143. fmt.Println(local_func_bar, local_func_int8_r_int8, local_func_int8_r_pint8, local_func_nil)
  144. fmt.Println(local_interface, local_interface_nil, local_interface_typed_nil)
  145. fmt.Println(local_map, local_map_2, local_map_3, local_map_empty, local_map_nil)
  146. fmt.Println(local_pointer, local_pointer_nil)
  147. fmt.Println(local_slice, local_slice_2, local_slice_nil)
  148. fmt.Println(local_string, local_struct)
  149. fmt.Println(local_unsafe_pointer, local_unsafe_pointer_nil)
  150. f1()
  151. f2()
  152. }
  153. func f1() {
  154. fmt.Println()
  155. }
  156. func f2() {
  157. fmt.Println()
  158. }
  159. func bar() {
  160. foo(42, 1.5)
  161. fmt.Print()
  162. }
  163. func populateChannels() {
  164. go func() {
  165. Z_channel_2 <- 8
  166. }()
  167. go func() {
  168. for i := int16(0); i < 14; i++ {
  169. Z_channel_buffered <- i
  170. }
  171. }()
  172. go func() {
  173. for i := 0; i < 8; i++ {
  174. <-Z_channel_buffered
  175. }
  176. }()
  177. time.Sleep(time.Second / 20)
  178. }
  179. func main() {
  180. args := os.Args[1:]
  181. expected := []string{"some", "arguments"}
  182. if len(args) != 2 || args[0] != expected[0] || args[1] != expected[1] {
  183. log.Fatalf("got command-line args %v, expected %v", args, expected)
  184. }
  185. populateChannels()
  186. for ; ; time.Sleep(2 * time.Second) {
  187. bar()
  188. }
  189. select {}
  190. }