Go Concepts - Functions: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 55: Line 55:
* Allocation: <tt>[[go Built-In Function new|new()]]</tt>
* Allocation: <tt>[[go Built-In Function new|new()]]</tt>
* Making slices, maps and channels: <tt>[[go Built-In Function make|make()]]</tt>
* Making slices, maps and channels: <tt>[[go Built-In Function make|make()]]</tt>
* Appending to and copying slices: [[go Built-In Functions Appending to and Copying Slices]] <tt>[[go Built-In Function append|append()]]</tt>, <tt>[[go Built-In Function copy|copy()]]</tt>
* Appending to and copying slices: <tt>[[go Built-In Functions Appending to and Copying Slices#append.28.29|append()]]</tt>, <tt>[[go Built-In Functions Appending to and Copying Slices#copy.28.29|copy()]]</tt>
* Deletion of map elements <tt>[[go Built-In Function delete|delete()]]</tt>
* Deletion of map elements <tt>[[go Built-In Function delete|delete()]]</tt>
* Handling panics <font color=red>panic, recover</font>
* Handling panics <font color=red>panic, recover</font>
Line 66: Line 66:


[[go Built-In Function copy]]
[[go Built-In Function copy]]
https://kb.novaordis.com/index.php/Go_Built-In_Functions_Appending_to_and_Copying_Slices#append.28.29


=Closures=
=Closures=

Revision as of 19:05, 22 March 2016

External

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. From this perspective, functions are conceptually different from methods.

Syntax

func <name>(
    [parameter-identifier1] [type1], [parameter-identifier2] [type2], ...) [(
    <return-type1>, [return-type2], ...)] {

   // function's body
   statement1
   statement2
   ...

   return <return-value1>, <return-value2>
}

The return type declarations are optional, if the function does not return anything.

A function may return one or more results.

Examples:

func add(a int, b int) (int) {
   return a + b;
}

Function Literals

The main() Function

Compiling an Executable

Built-in Functions

The built-in functions are available by default, without the need to import any package. They are what the specification calls pre-defined function identifiers. Their semantics depends on the arguments.

  • Length and capacity len(), cap()
  • close()
  • Allocation: new()
  • Making slices, maps and channels: make()
  • Appending to and copying slices: append(), copy()
  • Deletion of map elements delete()
  • Handling panics panic, recover
  • Manipulating complex numbers: complex, real, imag
  • Bootstrapping print, println

Delete

go Built-In Function append

go Built-In Function copy


https://kb.novaordis.com/index.php/Go_Built-In_Functions_Appending_to_and_Copying_Slices#append.28.29

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. From this point of view, the method is conceptually different from a function.

A method is always exported by the package it is enclosed in.

Syntax

func <receiver> <method-name> (...) {

    // the rest of declaration is similar to a function's
    ...
}

Difference between Functions and Methods

Receivers

Value Receivers

Pointer Receivers