|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "sync/atomic" |
| 7 | + "unsafe" |
| 8 | +) |
| 9 | + |
| 10 | +func main() { |
| 11 | + testGeneric() |
| 12 | + testNamed1() |
| 13 | + testNamed2() |
| 14 | + testNamed3() |
| 15 | + testAnonymous1() |
| 16 | + testAnonymous2() |
| 17 | + testAnonymous3() |
| 18 | + testAnonymous4() |
| 19 | + testAnonymous5() |
| 20 | + testAnonymous6() |
| 21 | + testAnonymous7() |
| 22 | + testAnonymous8() |
| 23 | + testAnonymousBuffer() |
| 24 | +} |
| 25 | + |
| 26 | +func testNamed1() { |
| 27 | + var a I = &T{100} |
| 28 | + if a.Demo1() != 100 { |
| 29 | + panic("testNamed1 error") |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func testNamed2() { |
| 34 | + var a I = T{100} |
| 35 | + if a.Demo1() != 100 { |
| 36 | + panic("testNamed2 error") |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func testNamed3() { |
| 41 | + var a I2 = &T{100} |
| 42 | + if a.Demo2() != 100 { |
| 43 | + panic("testNamed4 error") |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +type Pointer[T any] struct { |
| 48 | + // Mention *T in a field to disallow conversion between Pointer types. |
| 49 | + // See go.dev/issue/56603 for more details. |
| 50 | + // Use *T, not T, to avoid spurious recursive type definition errors. |
| 51 | + _ [0]*T |
| 52 | + v unsafe.Pointer |
| 53 | +} |
| 54 | + |
| 55 | +// Load atomically loads and returns the value stored in x. |
| 56 | +func (x *Pointer[T]) Load() *T { return (*T)(atomic.LoadPointer(&x.v)) } |
| 57 | + |
| 58 | +// Store atomically stores val into x. |
| 59 | +func (x *Pointer[T]) Store(val *T) { atomic.StorePointer(&x.v, unsafe.Pointer(val)) } |
| 60 | + |
| 61 | +type IP interface { |
| 62 | + Store(*any) |
| 63 | + Load() *any |
| 64 | +} |
| 65 | + |
| 66 | +func testGeneric() { |
| 67 | + var p IP = &Pointer[any]{} |
| 68 | + p.Store(func() *any { |
| 69 | + var a any = 100 |
| 70 | + return &a |
| 71 | + }()) |
| 72 | + if (*p.Load()).(int) != 100 { |
| 73 | + panic("testGeneric error") |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func testAnonymous1() { |
| 78 | + var s I = &struct { |
| 79 | + m int |
| 80 | + *T |
| 81 | + }{10, &T{100}} |
| 82 | + if s.Demo1() != 100 { |
| 83 | + panic("testAnonymous1 error") |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func testAnonymous2() { |
| 88 | + var s I = struct { |
| 89 | + m int |
| 90 | + *T |
| 91 | + }{10, &T{100}} |
| 92 | + if s.Demo1() != 100 { |
| 93 | + panic("testAnonymous2 error") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func testAnonymous3() { |
| 98 | + var s I = struct { |
| 99 | + m int |
| 100 | + T |
| 101 | + }{10, T{100}} |
| 102 | + if s.Demo1() != 100 { |
| 103 | + panic("testAnonymous3 error") |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func testAnonymous4() { |
| 108 | + var s I = &struct { |
| 109 | + m int |
| 110 | + T |
| 111 | + }{10, T{100}} |
| 112 | + if s.Demo1() != 100 { |
| 113 | + panic("testAnonymous4 error") |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func testAnonymous5() { |
| 118 | + var s I2 = &struct { |
| 119 | + m int |
| 120 | + T |
| 121 | + }{10, T{100}} |
| 122 | + if s.Demo2() != 100 { |
| 123 | + panic("testAnonymous5 error") |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func testAnonymous6() { |
| 128 | + var s I2 = struct { |
| 129 | + m int |
| 130 | + *T |
| 131 | + }{10, &T{100}} |
| 132 | + if s.Demo2() != 100 { |
| 133 | + panic("testAnonymous6 error") |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func testAnonymous7() { |
| 138 | + var s interface { |
| 139 | + Demo1() int |
| 140 | + Demo2() int |
| 141 | + } = struct { |
| 142 | + m int |
| 143 | + *T |
| 144 | + }{10, &T{100}} |
| 145 | + if s.Demo1() != 100 { |
| 146 | + panic("testAnonymous7 error") |
| 147 | + } |
| 148 | + if s.Demo2() != 100 { |
| 149 | + panic("testAnonymous7 error") |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func testAnonymous8() { |
| 154 | + var s interface { |
| 155 | + Demo1() int |
| 156 | + Demo2() int |
| 157 | + demo3() int |
| 158 | + } = struct { |
| 159 | + m int |
| 160 | + *T |
| 161 | + }{10, &T{100}} |
| 162 | + if s.Demo1() != 100 { |
| 163 | + panic("testAnonymous8 error") |
| 164 | + } |
| 165 | + if s.Demo2() != 100 { |
| 166 | + panic("testAnonymous8 error") |
| 167 | + } |
| 168 | + if s.demo3() != 100 { |
| 169 | + panic("testAnonymous8 error") |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +func testAnonymousBuffer() { |
| 174 | + var s fmt.Stringer = &struct { |
| 175 | + m int |
| 176 | + *bytes.Buffer |
| 177 | + }{10, bytes.NewBufferString("hello")} |
| 178 | + if s.String() != "hello" { |
| 179 | + panic("testAnonymousBuffer error") |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +type T struct { |
| 184 | + n int |
| 185 | +} |
| 186 | + |
| 187 | +func (t T) Demo1() int { |
| 188 | + return t.n |
| 189 | +} |
| 190 | + |
| 191 | +func (t *T) Demo2() int { |
| 192 | + return t.n |
| 193 | +} |
| 194 | + |
| 195 | +func (t *T) demo3() int { |
| 196 | + return t.n |
| 197 | +} |
| 198 | + |
| 199 | +type I interface { |
| 200 | + Demo1() int |
| 201 | +} |
| 202 | + |
| 203 | +type I2 interface { |
| 204 | + Demo2() int |
| 205 | +} |
0 commit comments