Type: Struct with methods

Like with simple types, we can add methods to structs.

Struct methods are functions that are attached to a type delcared outside of the structure.


func (p Person) Greet() string {
    return "Hello, my name is" + p.Name
}

func main() {
    p := Person{"John", 30}
    msg := (p.Greet())
}









TRY IT OUT

Rob Pike


Exercise: Create a data structure for a course with instructor

Create structs for a course with an instructor.

The instructor should have a first name, last name.

Implement a method that prints the first name and last name of an instructor.


Note: The struct method can be defined in any file within the package.