| 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 ( |
| 10 |
"fmt" |
| 11 |
"strconv" |
| 12 |
) |
| 13 |
|
| 14 |
func test1[T any](fn func(T) int, v T) int { |
| 15 |
fn1 := func() int { |
| 16 |
var i interface{} = v |
| 17 |
val := fn(i.(T)) |
| 18 |
return val |
| 19 |
} |
| 20 |
return fn1() |
| 21 |
} |
| 22 |
|
| 23 |
func main() { |
| 24 |
want := 123 |
| 25 |
got := test1(func(s string) int { |
| 26 |
r, err := strconv.Atoi(s) |
| 27 |
if err != nil { |
| 28 |
return 0 |
| 29 |
} |
| 30 |
return r |
| 31 |
}, "123") |
| 32 |
if got != want { |
| 33 |
panic(fmt.Sprintf("got %f, want %f", got, want)) |
| 34 |
} |
| 35 |
} |