interfaces/

directory
v0.0.0-...-b3f521c Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2017 License: Apache-2.0

README

Interfaces

Interfaces provide a way to declare types that define only behavior. This behavior can be implemented by concrete types, such as struct or named types, via methods. When a concrete type implements the set of methods for an interface, values of the concrete type can be assigned to variables of the interface type. Then method calls against the interface value actually call into the equivalent method of the concrete value. Since any concrete type can implement any interface, method calls against an interface value are polymorphic in nature.

Notes

  • The method set for a value, only includes methods implemented with a value receiver.
  • The method set for a pointer, includes methods implemented with both pointer and value receivers.
  • Methods declared with a pointer receiver, only implement the interface with pointer values.
  • Methods declared with a value receiver, implement the interface with both a value and pointer receiver.
  • The rules of method sets apply to interface types.
  • Interfaces are reference types, don't share with a pointer.
  • This is how we create polymorphic behavior in go.

https://golang.org/doc/effective_go.html#interfaces
http://blog.golang.org/laws-of-reflection
http://www.goinggo.net/2014/05/methods-interfaces-and-embedded-types.html
Interface Pollution
Abstraction Considered Harmful

Code Review

"Polymorphsim means that you write a certain program and it behaves differently depending on the data that it operates on." - Tom Kurtz (inventor of BASIC)

Polymorphism (Go Playground)
Method Sets (Go Playground)
Address Of Value (Go Playground)
Behavior Changes (Go Playground)
Storage By Value (Go Playground)

Advanced Code Review

Storing Values (Go Playground)

Exercises

Exercise 1

Part A Declare an interface named speaker with a method named speak. Declare a struct named english that represents a person who speaks english and declare a struct named chinese for someone who speaks chinese. Implement the speaker interface for each struct using a value receiver and these literal strings "Hello World" and "你好世界". Declare a variable of type speaker and assign the address of a value of type english and call the method. Do it again for a value of type chinese.

Part B Add a new function named sayHello that accepts a value of type speaker. Implement that function to call the speak method on the interface value. Then create new values of each type and use the function.

Template (Go Playground) | Answer (Go Playground)


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

Directories

Path Synopsis
advanced
example1
Sample program that explores how interface assignments work when values are stored inside the interface.
Sample program that explores how interface assignments work when values are stored inside the interface.
Sample program to show how polymorphic behavior with interfaces.
Sample program to show how polymorphic behavior with interfaces.
Sample program to show how to understand method sets.
Sample program to show how to understand method sets.
Sample program to show how you can't always get the address of a value.
Sample program to show how you can't always get the address of a value.
Sample program to show how method sets can affect behavior.
Sample program to show how method sets can affect behavior.
Sample program to show how the concrete value assigned to the interface is what is stored inside the interface.
Sample program to show how the concrete value assigned to the interface is what is stored inside the interface.
exercises
exercise1
Declare an interface named speaker with a method named speak.
Declare an interface named speaker with a method named speak.
template1
Declare an interface named speaker with a method named speak.
Declare an interface named speaker with a method named speak.

Jump to

Keyboard shortcuts

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