Created
April 7, 2019 21:38
-
-
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
This file contains 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" | |
) | |
/* | |
* 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