I know I can write code like this, but I don't know how it works:
type MyTransport struct {
http.Transport
}
func (myT *MyTransport) RoundTrip(r *http.Request) (*http.Response, error) {
return myT.Transport.RoundTrip(r)
}
http.Transport
is just a struct, right? It has no name. So how does myT.Transport
work? Why do I not have to give the transport a name in MyTransport
, such as declaring it like ht http.Transport
?
What is a struct? A struct is a user-defined type that represents a collection of fields. It can be used in places where it makes sense to group the data into a single unit rather than having each of them as separate values. For instance, an employee has a firstName, lastName and age.
Go struct is a user-defined type that represents the collection of fields. It can be used in the places where it makes sense to group data into the single unit rather than maintaining each of them as the separate types.
A struct (short for "structure") is a collection of data fields with declared data types. Golang has the ability to declare and create own data types by combining one or more types, including both built-in and user-defined types. Each data field in a struct is declared with a known type, which could be a built-in type or another user-defined type.
The above Employee struct is called a named struct because it creates a new data type named Employee using which Employee structs can be created. This struct can also be made more compact by declaring fields that belong to the same type in a single line followed by the type name.
That's an embedded struct, from http://golang.org/doc/effective_go.html#embedding :
By embedding the structs directly, we avoid this bookkeeping. The methods of embedded types come along for free, which means that bufio.ReadWriter not only has the methods of bufio.Reader and bufio.Writer, it also satisfies all three interfaces: io.Reader, io.Writer, and io.ReadWriter.
There's an important way in which embedding differs from subclassing. When we embed a type, the methods of that type become methods of the outer type, but when they are invoked the receiver of the method is the inner type, not the outer one. In our example, when the Read method of a bufio.ReadWriter is invoked, it has exactly the same effect as the forwarding method written out above; the receiver is the reader field of the ReadWriter, not the ReadWriter itself.
Embedding can also be a simple convenience. This example shows an embedded field alongside a regular, named field.
TL;DR:
That's go's way of "oop" inheritance, more or less:
type MyTransport struct {
http.Transport
}
//without this function, calling myT.RoundTrip would actually call myT.Transport.RoundTrip
func (myT *MyTransport) RoundTrip(r *http.Request) (*http.Response, error) {
return myT.Transport.RoundTrip(r)
}
This is known as embedding. If you haven't read Effective Go, you should. http://golang.org/doc/effective_go.html#embedding
In a nutshell, the methods of the embedded http.Transport
are accessible through MyTransport
struct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With