Back to Gotraining

README

topics/go/language/functions/README.md

latest1.9 KB
Original Source

Functions

Functions are at the core of the language. They provide a mechanism to group and organize our code to separate and distinct pieces of functionality. They can be used to provide an API to the packages we write and are a core component to concurrency.

Notes

  • Functions can return multiple values and most return an error value.
  • The error value should always be checked as part of the programming logic.
  • The blank identifier can be used to ignore return values.

https://golang.org/doc/effective_go.html#functions
https://www.ardanlabs.com/blog/2013/10/functions-and-naked-returns-in-go.html
https://www.ardanlabs.com/blog/2013/06/understanding-defer-panic-and-recover.html

Code Review

Return multiple values (Go Playground)
Blank identifier (Go Playground)
Redeclarations (Go Playground)
Anonymous Functions/Closures (Go Playground)

Advanced Code Review

Recover panics (Go Playground)

Exercises

Exercise 1

Part A Declare a struct type to maintain information about a user. Declare a function that creates value of and returns pointers of this type and an error value. Call this function from main and display the value.

Part B Make a second call to your function but this time ignore the value and just test the error value.

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.