Go Concepts - Functions: Difference between revisions
Jump to navigation
Jump to search
Line 9: | Line 9: | ||
=Function Definition= | =Function Definition= | ||
A function declaration starts with the <tt>[[Go Keyword func|func]]</tt> keyword and it consists in a series of [[Go Concepts - Lexical Structure#Statements|statements]] applied on a number of inputs, resulting in a number of outputs. Same inputs will always result in the same outputs. ''Function do not depend on a type instance's state''. | A function declaration starts with the <tt>[[Go Keyword func|func]]</tt> keyword and it consists in a series of [[Go Concepts - Lexical Structure#Statements|statements]] applied on a number of inputs, resulting in a number of outputs. Same inputs will always result in the same outputs. ''Function do not depend on a type instance's state''. Functions are conceptually different from [[#Method_Defiition|methods]]. | ||
=Syntax= | =Syntax= |
Revision as of 06:38, 22 March 2016
External
- Function type specification https://golang.org/ref/spec#Function_types
Internal
Function Definition
A function declaration starts with the func keyword and it consists in a series of statements applied on a number of inputs, resulting in a number of outputs. Same inputs will always result in the same outputs. Function do not depend on a type instance's state. Functions are conceptually different from methods.
Syntax
func [receiver] <name>( [parameter-identifier1] [type1], [parameter-identifier2] [type2], ...) ( [return-type1], [return-type2], ...) { statement1 statement2 ... return <return-value1>, <return-value2> }
Examples:
func add(a int, b int) (int) { return a + b; }
Built-in Functions
make() | append() | copy() | delete() | . |
new() | len() | cap() | . | . |
. | . | . | . | . |
Closures
Anonymous function that capture local variables.
More about closures is available here.
Methods
A method defines the behavior of a type, and it relies on the state of an instance of the type. The method will - and it is supposed to - change the state.
A method is always exported by the package it is enclosed in.