GoConqr has a new look! Let us know what you think.

Mind map

Functions

Filipi Melo Learning Go Book English

Map content

Functions

  1. Declaring and Calling Functions

    • Function declaration
      • func keyword, function name, input parameters (with types), return type.
    • Return keyword
      • Needed only if the function returns a value or exits early.
    • No input parameters
      • Use ()
    • Leave the return type empty.
    • Multiple parameters of the same type
      • Specify type once for all.
    • Simulating Named and Optional Parameters
      • No named/optional parameters in Go.
      • Emulate with structs
        • Define a struct with fields for desired parameters.
      • Few parameters are best
        • More parameters often indicate overly complex functions.
    • Variadic Input Parameters and Slices
      • Variadic parameters
        • Indicated by ... before the type, must be the last parameter.
      • Variadic parameter as a slice
        • Use like any slice.
      • Supplying a slice
        • Append ... to the variable/slice literal; omitting it causes a compile-time error.
    • Multiple Return Values
      • Multiple return values
        • Types listed in parentheses, separated by commas.
      • Returning multiple values
        • Must return all values, separated by commas.
      • Error handling
        • Conventionally, the last return value is an error, nil if no error.
    • Multiple Return Values Are Multiple Values
      • Assign all values
        • Must assign each returned value to a variable.
      • Ignoring return values
        • Use _ for values you don't need.
    • Ignoring Returned Values
      • No unused variables allowed.
      • Use _
        • Assign unused return values to _.
      • Implicitly ignoring values
        • Possible by omitting assignment, except when clarity is needed.
    • Named Return Values
      • Predeclare variables within the function for return values.
      • Initialization
        • Named return values are initialized to their zero values.
      • Partial naming
        • Use _ for unnamed return values.
      • Local scope
        • Names are local to the function, no enforcement outside.
      • Shadowing
        • Be cautious of accidentally shadowing named return values.
    • Blank Returns—Never Use These!
      • Considered bad practice as they obscure data flow and make code harder to understand.
      • Using named return values allows returning without specifying values.
  2. Functions Are Values

    • Functions as values:
      • Can be assigned to variables.
    • Function type
      • Defined by func keyword, parameter, and return types.
    • Default value
      • Function variables default to nil
      • Running a nil function causes a panic.
    • Anonymous Functions
      • Functions without names
      • Defined with func keyword
      • Can be assigned to variables
      • Inline usage
        • Can be declared and called immediately.
      • Use cases
        • Useful for defer statements and launching goroutines.
  3. Closures

    • Functions declared inside other functions that can access and modify outer variables.
    • Scope limitation
      • Useful for reducing global declarations and simplifying code.
    • Passing/returning closures
      • Allows external access to inner function variables.
    • Passing Functions as Parameters
      • Functions can be passed to other functions for flexible operations.
  4. defer

    • Defer keyword
      • Used for cleanup code that runs after a function completes, regardless of exit point.
  5. Go Is Call by Value

    • Call-by-value
      • Go copies variable values when passing to functions.
    • Maps and slices
      • Behave differently due to implementation with pointers.
    • Call-by-value benefits
      • Functions don't modify input variables
      • Enhancing data flow understanding

Information

Description
Book Learning Go (CH5: Functions) Mind Map on Functions, created by Filipi Melo on 23/10/2024.
Created by
Filipi Melo
Views
0
Rating

More on Go Programming

Feedback

What is it about?

What do you think of the new look?

How do you like it?