methods/

directory
v0.0.0-...-1ea3785 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2015 License: Apache-2.0

README

Methods

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

Notes

  • Methods are functions that contain a receiver value.
  • Receivers bind a method to a type and can be value or pointers.
  • Methods are called against values and pointers, not packages.
  • Go support function and method variables.

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)

Named typed methods (Go Playground)

Advanced Code Review

Function/Method variables (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
advanced
example1
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 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.
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