Join David Gassner for an in-depth discussion in this video Defining functions as methods of custom types, part of Learning Go.
- As we've seen, Go is organized…around packages with functions.…As with all object-oriented languages, though,…Go also supports the concept of methods,…which are essentially functions,…but they're owned by some structure…or other type.…In a more completely object-oriented language…such as Java,…you'd say that each method is a member of a class.…In Go, a method is a member of a type.…In this example, I'm going to show you…how to attach a method to a struct,…which is one sort of type,…and I'm working with this version of a structure…named Dog.…
It has three fields named Breed, Weight,…and I've added a new field named Sound.…Now to add a method, declare a separate function…but add the type of the function's owner…before the function name with an identifier…wrapped in parentheses, like this.…I'll start with the keyword func…and then in parentheses first a unique identifier…or a variable name,…and then the data type…that I'm attaching the method to.…Then after that, define your method.…
This will be a simple method…that doesn't receive any arguments,…
- 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.