github.com/gbl08ma/monkey@v1.1.0/examples/instance_example.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"net/http"
     7  	"reflect"
     8  
     9  	"github.com/gbl08ma/monkey"
    10  )
    11  
    12  func main() {
    13  	var d *net.Dialer
    14  	monkey.PatchInstanceMethod(reflect.TypeOf(d), "Dial", func(_ *net.Dialer, _, _ string) (net.Conn, error) {
    15  		return nil, fmt.Errorf("no dialing allowed")
    16  	})
    17  	_, err := http.Get("http://google.com")
    18  	fmt.Println(err) // Get http://google.com: no dialing allowed
    19  }