notexOPENOverview
Notex/Languages/Functions

Functions

Always free to read

The basics of Go functions are familiar to anyone who has programmed in other languages with first-class functions, like C, Python, Ruby, or JavaScript. Just as with control…

03

GO / CORE LANGUAGE

Functions

18 LESSONS · 40 MIN
  1. 03.01Declaring and Calling FunctionsThe basics of Go functions are familiar to anyone who has programmed in other languages with first-class functions, like C, Python, Ruby, or JavaScript. Just as with control…READING2 MIN
  2. 03.02Simulating Named and Optional ParametersBefore getting to Go’s unique function features, I’ll mention two that Go doesn’t have: named and optional input parameters. With one exception, which I will cover in the next…READING1 MIN
  3. 03.03Variadic Input Parameters and SlicesYou’ve been using fmt.Println to print results to the screen, and you’ve probably noticed that this function allows any number of input parameters. How does it do that?READING1 MIN
  4. 03.04Multiple Return ValuesThe first difference that you’ll see between Go and other languages is that Go allows for multiple return values. Let’s add a small feature to the previous division program.…READING2 MIN
  5. 03.05Ignoring Returned ValuesWhat if you call a function and don’t want to use all the returned values? As covered in “Unused Variables”, Go does not allow unused variables. If a function returns multiple…READING1 MIN
  6. 03.06Named Return ValuesIn addition to letting you return more than one value from a function, Go allows you to specify names for your return values. Rewrite the divAndRemainder function one more…READING2 MIN
  7. 03.07Blank Returns—Never Use These!If you use named return values, you need to be aware of one severe misfeature in Go: blank (sometimes called naked) returns. If you have named return values, you can just write…READING1 MIN
  8. 03.08Code Review: functions, parameters, and return valuesA function shouldn’t have a lot of parameters. Make sure your function isn’t doing too much.PRACTICE1 MIN
  9. 03.09Practice: functions, parameters, and return valuesThese exercises let you experiment with writing functions of your own.PRACTICE1 MIN
  10. 03.10Functions Are ValuesJust as in many other languages, functions in Go are values. The type of a function is built out of the keyword func and the types of the parameters and return values. This…READING6 MIN
  11. 03.11ClosuresFunctions declared inside functions are special; they are called closures. This is a computer-science word that means that functions declared inside functions can access and…READING7 MIN
  12. 03.12Code Review: Function values and closuresIf multiple functions with the same signature are used for similar purposes and assigned to variables, define a function type and use it when declaring the function variables.…PRACTICE1 MIN
  13. 03.13Practice: Function values and closuresThese exercises test your knowledge of function values and closures.PRACTICE1 MIN
  14. 03.14Cleaning up with deferPrograms often create temporary resources, like files or network connections, that need to be cleaned up, no matter how many exit points the function has, or whether it…READING7 MIN
  15. 03.15Go Is Call by ValueYou might hear people call Go a call-by-value language. This means that when you supply a variable for a parameter to a function, Go always makes a copy of the variable’s…READING3 MIN
  16. 03.16Code Review: defer and call by valueUse defer to make sure that resources are cleaned up when a function exits.PRACTICE1 MIN
  17. 03.17Practice: defer and call by valueThese exercises test your knowledge of defer statments and call by value in Go.PRACTICE1 MIN
  18. 03.18Recap: FunctionsIn this topic, you’ve learned how functions in Go are similar to functions in other languages, as well as their unique features. In this topic, you’ll find out that pointers…READING1 MIN
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K