methods/

directory
v0.0.0-...-86d22a7 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2017 License: Apache-2.0

README

Methods

Methods are functions that are declared with a receiver which binds the method to a type. Methods can be used to operate on values or pointers of that type.

Notes

  • Methods are functions that declare a receiver variable.
  • Receivers bind a method to a type and can use value or pointer semantics.
  • Value semantics mean a copy of the value is passed across program boundaries.
  • Pointer semantics mean a copy of the values address is passed across program boundaries.
  • Stick to a single semantic for a given type and be consistent.

Quotes

"Methods are valid when it is practical or reasonable for a piece of data to expose a capability." - William Kennedy

https://golang.org/doc/effective_go.html#methods
http://www.goinggo.net/2014/05/methods-interfaces-and-embedded-types.html

Code Review

Declare and receiver behavior (Go Playground)
Value and Pointer semantics (Go Playground)
Named typed methods (Go Playground)
Function/Method variables (Go Playground)
Function Types (Go Playground)

Exercises

Exercise 1

Declare a struct that represents a baseball player. Include name, atBats and hits. Declare a method that calculates a players batting average. The formula is Hits / AtBats. Declare a slice of this type and initialize the slice with several players. Iterate over the slice displaying the players name and batting average.

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 to declare methods and how the Go compiler supports them.
Sample program to show how to declare methods and how the Go compiler supports them.
Sample program to show how to declare methods against a named type.
Sample program to show how to declare methods against a named type.
Sample program to show how to declare function variables.
Sample program to show how to declare function variables.
Sample program to show how to declare and use function types.
Sample program to show how to declare and use function types.
exercises
exercise1
Declare a struct that represents a baseball player.
Declare a struct that represents a baseball player.
template1
Declare a struct that represents a baseball player.
Declare a struct that represents a baseball player.

Jump to

Keyboard shortcuts

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