Join David Gassner for an in-depth discussion in this video Returning multiple values from functions, part of Learning Go.
- Functions in Go can return multiple values.…You can see this in the fmt package…where many of the functions return an integer and an error.…For example, the Fprint function receives a Writer,…and then a number of items indicated by a...interface.…And then it returns an integer value and an error object.…And if you look at the documentation you'll see…that the integer is the number of bytes written…and the error is any error that's encountered.…Notice the signature of the function.…
The return value is wrapped in parentheses,…and then the two values that are being returned…are declared in a comma delimited list.…The ability to return multiple values like this…is essential in a language that doesn't have…conventional structured exception handling syntax.…Instead of throwing an exception,…and then handling it in a try-catch block,…the function returns the expected value and the error.…It's then up to the developer to evaluate…the returned values and decide what to do.…
You can create your own custom functions…
- Installing Go tools
- Creating and compiling a Go workspace
- Exploring variables, constants, and types
- Storing ordered and unordered values
- Grouping related values in structs
- Programming conditional logic and loops
- Defining and calling functions
- Handling errors
- Working with files
- Creating a simple HTTP server
Skill Level Beginner
1. Getting Started
2. Essential Go Skills
3. Managing Simple Values
4. Managing Complex Types and Collections
5. Managing Program Flow
6. Structuring Go Code
7. Working with Files and the Web
Next steps2m 26s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.