Skip to content

Instantly share code, notes, and snippets.

@tevin-morake
Created April 7, 2019 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tevin-morake/21af4faf0e55d3174baebd3400c3dd04 to your computer and use it in GitHub Desktop.
Save tevin-morake/21af4faf0e55d3174baebd3400c3dd04 to your computer and use it in GitHub Desktop.
Illustrates how to use embedded structs and access fields within embedded structs
package main
import (
"fmt"
)
/*
* With Embedded structs,the embedded fields are promoted to the parent struct
* For Example, the programmer struct is embedded within the staffMembers struct.
* This means that if I want to access the Stack field values from programmer struct, I do not need to go :
-> pg1.programmer.Stack
-> I can just access it by going pg1.Stack
*/
type programmer struct {
PName string
Stack string
}
type staffMembers struct {
programmer
employed bool
}
func main() {
pg1 := staffMembers{
programmer: programmer{
PName: "Rock-Lee",
Stack: "JS, HTML5, CSS3, JQUERY",
}, employed: true,
}
fmt.Println(pg1.Stack,"\n", pg1.PName)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment