Created
September 4, 2018 13:22
-
-
Save viveksyngh/21be95734db27883761b355debfd4184 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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