| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2021 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import "reflect" |
| 10 |
|
| 11 |
type Foo[T any] struct { |
| 12 |
val int |
| 13 |
} |
| 14 |
|
| 15 |
func (foo Foo[T]) Get() *T { |
| 16 |
if foo.val != 1 { |
| 17 |
panic("bad val field in Foo receiver") |
| 18 |
} |
| 19 |
return new(T) |
| 20 |
} |
| 21 |
|
| 22 |
var ( |
| 23 |
newInt = Foo[int]{val: 1}.Get |
| 24 |
newString = Foo[string]{val: 1}.Get |
| 25 |
) |
| 26 |
|
| 27 |
func main() { |
| 28 |
i := newInt() |
| 29 |
s := newString() |
| 30 |
|
| 31 |
if t := reflect.TypeOf(i).String(); t != "*int" { |
| 32 |
panic(t) |
| 33 |
} |
| 34 |
if t := reflect.TypeOf(s).String(); t != "*string" { |
| 35 |
panic(t) |
| 36 |
} |
| 37 |
} |