Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to specify just a struct type as a struct member in Go?

Tags:

go

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?

like image 856
cflewis Avatar asked Jun 06 '14 21:06

cflewis


People also ask

What is a struct in C++?

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.

What is go struct in C++?

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.

What is a struct in Golang?

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.

Why employee struct is called a named struct?

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.


2 Answers

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)
}
like image 124
OneOfOne Avatar answered Sep 30 '22 23:09

OneOfOne


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.

like image 41
zmb Avatar answered Sep 30 '22 23:09

zmb