| 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 |
//go:build cgo |
| 8 |
|
| 9 |
package main |
| 10 |
|
| 11 |
import "runtime/cgo" |
| 12 |
|
| 13 |
type iface interface { |
| 14 |
Get() int |
| 15 |
} |
| 16 |
|
| 17 |
type notInHeap struct { |
| 18 |
_ cgo.Incomplete |
| 19 |
i int |
| 20 |
} |
| 21 |
|
| 22 |
type myInt struct { |
| 23 |
f *notInHeap |
| 24 |
} |
| 25 |
|
| 26 |
func (mi myInt) Get() int { |
| 27 |
return int(mi.f.i) |
| 28 |
} |
| 29 |
|
| 30 |
type embed struct { |
| 31 |
*myInt |
| 32 |
} |
| 33 |
|
| 34 |
var val = 1234 |
| 35 |
|
| 36 |
var valNotInHeap = notInHeap{i: val} |
| 37 |
|
| 38 |
func main() { |
| 39 |
i := val |
| 40 |
check(i) |
| 41 |
mi := myInt{f: &valNotInHeap} |
| 42 |
check(mi.Get()) |
| 43 |
ifv := iface(mi) |
| 44 |
check(ifv.Get()) |
| 45 |
ifv = iface(&mi) |
| 46 |
check(ifv.Get()) |
| 47 |
em := embed{&mi} |
| 48 |
check(em.Get()) |
| 49 |
ifv = em |
| 50 |
check(ifv.Get()) |
| 51 |
ifv = &em |
| 52 |
check(ifv.Get()) |
| 53 |
} |
| 54 |
|
| 55 |
func check(v int) { |
| 56 |
if v != val { |
| 57 |
panic(v) |
| 58 |
} |
| 59 |
} |