02-interfaces/

directory
v0.0.0-...-35bbffe Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2015 License: Apache-2.0

README

Interfaces - Methods, Interfaces and Embedding

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 reciever.
  • The method set for a pointer, includes methods implemented with both pointer and value recievers.
  • 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

https://medium.com/@rakyll/interface-pollution-in-go-7d58bccec275

Code Review

Method Receivers (Go Playground)

Polymorphism (Go Playground)

Address Of Value (Go Playground)

Exercises

Exercise 1

Part A Declare an interface named Speaker with a method named SayHello. 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 SayHello method on the interface value. Then create new values of each type and use the function.

Template (Go Playground) | Answer (Go Playground)


Ardan Labs Ardan Studios GoingGo Blog


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

Directories

Path Synopsis
Sample program to show how to use an interface in Go.
Sample program to show how to use an interface in Go.
Sample program to show how polymorphic behavior with interfaces.
Sample program to show how polymorphic behavior with interfaces.
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.
exercises
exercise1
Declare an interface named speaker with a method named sayHello.
Declare an interface named speaker with a method named sayHello.
template1
Declare an interface named speaker with a method named sayHello.
Declare an interface named speaker with a method named sayHello.

Jump to

Keyboard shortcuts

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