Create a struct that implements an interface. Encapsulate it into another struct. Fetch the struct / dereference it.
- Fetching gets pointer to the implementation of the interface.
- Dereferencing the pointer results in another point to the interface.
Why wouldn't dereferencing the pointer to the implementation gives the implementation instance?
package main
import (
"fmt"
"net/http"
"golang.org/x/net/http2"
)
func main() {
transport := &http2.Transport{}
client := &http.Client{Transport: transport}
tmp1 := client.Transport
tmp2 := &client.Transport
tmp3 := &tmp1
fmt.Printf("%T
%T
%T
", tmp1, tmp2, tmp3)
//dialTLS := tmp1.DialTLS
}
This code outputs...
*http2.Transport
*http.RoundTripper
*http.RoundTripper
As well, trying to access a property of the tmp1
(uncommenting dialTLS := tmp1.DialTLS
) results in compile error...
tmp1.DialTLS undefined (type http.RoundTripper has no field or method DialTLS)
...even though fmt.Printf("%+v", tmp1)
outputs...
&{DialTLS:<nil> TLSClientConfig:0xc8203ec8c0 ConnPool:<nil> DisableCompression:false MaxHeaderListSize:0 t1:<nil> connPoolOnce:{m:{state:0 sema:0} done:0} connPoolOrDef:<nil>}
What I am attempting to do is access DialTLS
in the Transport instance.