Skip to content

Instantly share code, notes, and snippets.

@viveksyngh
Created September 4, 2018 13:22
Show Gist options
  • Save viveksyngh/21be95734db27883761b355debfd4184 to your computer and use it in GitHub Desktop.
Save viveksyngh/21be95734db27883761b355debfd4184 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
//Person struct to represent a person
type Person struct {
FirstName string
LastName string
}
func (p Person) getFullName() string {
return p.FirstName + " " + p.LastName
}
func (p Person) introduce() string {
return fmt.Sprintf("Hi !! My name is %s %s.", p.FirstName, p.LastName)
}
//Student struct to represent a student
type Student struct {
Person //A `Person` type field without any name
University string
}
func (s Student) introduce() string {
return fmt.Sprintf("Hi !! My name is %s %s. I am student at %s.", s.FirstName, s.LastName, s.University)
}
func main() {
p := Person{FirstName: "John", LastName: "Doe"}
s := Student{Person: p, University: "Stanford"}
fmt.Printf("Student Introduction: %s\n", s.introduce())
fmt.Printf("Person Introduction: %s\n", s.Person.introduce())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment