Created
May 29, 2017 14:37
-
-
Save girish3/e75f1b6f455e1f1dee718c0f2b7f4b9c 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
// Go’s syntax is very much like C, you should be fine | |
// reading it. | |
// Defining Human type, it has a variable Name of type string. | |
// (Yes, type is mentioned after variable name) | |
type Human struct { | |
Name string | |
} | |
// Defining a method GetName on the type Human which | |
// returns a string. | |
func (h Human) GetName() string { | |
return h.Name | |
} | |
// Human struct is embedded within Student, this is not | |
// inheritance but composition. Composition can also be done | |
// in a usual way by creating a Human type variable but there | |
// are few advantages to using struct embedding. | |
type Student struct { | |
id int | |
Human | |
} | |
human := Human{"John"} // type is implicit | |
student := Student{human, 1} | |
// you can actually do the following, even though getName() is | |
// Human's method. | |
student.GetName() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment