'Monkey Patching in Go show different result when running from CLI

I am using Monkey Patching in Go. When I debug the following code in VSCode it shows that the function proc.Signal return the error programmed.

func TestCheckProcessRunning(t *testing.T) {
    monkey.Patch((*os.Process).Signal, func(p *os.Process, sig os.Signal) error {
        return errors.New("Signal failed")
    })
    proc := &os.Process{}
    sig_e := proc.Signal(syscall.Signal(0))
    fmt.Printf("%s\n", sig_e)

}
Signal failed

But when I tried to run the test using go test . , the patch is no longer applied and got a different error:

os: process not initialized 

Any idea of what I am doing wrong?



Solution 1:[1]

It seems it needed the flag -gcflags=-l

go test -gcflags=-l .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Odd Kviteberg