I have some code that I've sat on for some time:
type Cell int
type Function func() error
func cellToFunction(c Cell) *Function {
p := unsafe.Pointer(uintptr(c))
if p == nil { return nil }
return (*Function)(p)
}
func functionToCell(w *Function) Cell {
return Cell(uintptr(unsafe.Pointer(w)))
}
It's not []byte, but the idea is similar. There are a few major gotchas.
After functionToCell, it's possible for the function to be garbage
collected. The function type must be known ahead of time for
cellToFunction. There's no way that I know to get arbitrary functions.
Honestly, this approach is fragile and error prone, but it is possible.
On Mon, Mar 20, 2017, 7:57 PM Sandeep Kalra <[email protected]> wrote:
> Is there any option to convert []byte to func (Assuming that []byte
> carries valid function)
> ----
> package main
>
> import (
> "fmt"
> "io/ioutil"
> )
>
> func run(b []byte) {
> /// HERE ?
> }
>
> func main() {
> if b, e := ioutil.ReadFile("./file.go"); e != nil {
> fmt.Println(e)
> } else {
> run(b)
> }
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.