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.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…
- 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…
- 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?
- 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.…
- 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…
- 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…
- 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…
- 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.
- 03.09Practice: functions, parameters, and return valuesThese exercises let you experiment with writing functions of your own.
- 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…
- 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…
- 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.…
- 03.13Practice: Function values and closuresThese exercises test your knowledge of function values and closures.
- 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…
- 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…
- 03.16Code Review: defer and call by valueUse defer to make sure that resources are cleaned up when a function exits.
- 03.17Practice: defer and call by valueThese exercises test your knowledge of defer statments and call by value in Go.
- 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…