是否可以从现有参考创建切片?

Let's say I have the following function:

func foo(bar []int) {
    bar[0] = 456
}

I want to create an []int with one element that references a, such that when I:

var a int = 10
// mySlice := ... crazy hackery?
foo(mySlice)

The value of a will be 456.


I know this is crazy and stupid and not at all the right thing to do. I'm not looking for the "correct" way. The function prototype for foo must remain the same.

I can guarantee that while the underlying int will change, the slice that points to it is immutable.

The reason for the strange requirement is because this is for the c2go project.

You can cast any types you want through an unsafe.Pointer.

First convert the *int to a pointer to an array, then slice it:

(*[1]int)(unsafe.Pointer(&a))[:]

https://play.golang.org/p/bmKcMIj3pb