replace struct's field value
Unsafe struct's field replacer.
$ go get github.com/podhmo/snatch
- debugging
- testing
Use Field()
or FieldOrPanic()
method.
package main
import (
"fmt"
"unsafe"
"github.com/podhmo/snatch"
"github.com/podhmo/snatch/examples/00readme/program"
)
func main() {
prog := program.New()
prog.Run()
fmt.Println("----------------------------------------")
snatch.FieldOrPanic(prog, "debug", func(ptr unsafe.Pointer) {
realPtr := (*bool)(ptr)
*realPtr = true
})
prog.Run()
}
Execution result.
Run, debug= false
----------------------------------------
Run, debug= true
Then, program package's code is here.
package program
import "fmt"
// Program ...
type Program struct {
debug bool
}
func (p *Program) Run() {
fmt.Println("Run, debug=", p.debug)
}
func New() *Program {
return &Program{}
}