embedding/

directory
v0.0.0-...-a855ea4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2016 License: Apache-2.0

README

Embedding

Embedding types provides the final piece of sharing and reusing state and behavior between types. Through the use of inner type promotion, an inner type's fields and methods can be directly access by references of the outer type.

Notes

  • Embedding types allow us to share state or behavior between types.
  • The inner type never loses its identity.
  • This is not inheritance.
  • Through promotion, inner type fields and methods can be accessed through the outer type.
  • The outer type can override the inner type's behavior.

http://www.goinggo.net/2014/05/methods-interfaces-and-embedded-types.html

Code Review

Declaring Fields (Go Playground)

Embedding types (Go Playground)

Embedded types and interfaces (Go Playground)

Outer and inner type interface implementations (Go Playground)

Exercises

Exercise 1

Copy the code from the template. Declare a new type called hockey which embeds the sports type. Implement the matcher interface for hockey. When implementing the match method for hockey, call into the match method for the embedded sport type to check the embedded fields first. Then create two hockey values inside the slice of matchers and perform the search.

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
Sample program to show how what we are doing is NOT embedding a type but just using a type as a field.
Sample program to show how what we are doing is NOT embedding a type but just using a type as a field.
Sample program to show how to embed a type into another type and the relationship between the inner and outer type.
Sample program to show how to embed a type into another type and the relationship between the inner and outer type.
Sample program to show how embedded types work with interfaces.
Sample program to show how embedded types work with interfaces.
Sample program to show what happens when the outer and inner type implement the same interface.
Sample program to show what happens when the outer and inner type implement the same interface.
exercises
exercise1
Copy the code from the template.
Copy the code from the template.
template1
Copy the code from the template.
Copy the code from the template.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL