// 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()