Mapa mental
Functions
Conteúdo do mapa
Functions
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.
- Variadic parameters
- 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
- 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.
- Assign all values
- 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.
- Function declaration
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.
- Functions as values:
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.
defer
- Defer keyword
- Used for cleanup code that runs after a function completes, regardless of exit point.
- Defer keyword
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
- Call-by-value
Informações
- Descrição
- Book Learning Go (CH5: Functions) Mapa Mental sobre Functions, criado por Filipi Melo em 23-10-2024.
- Criado por
- Filipi Melo
- Visualizações
- 0
- Avaliação