Created
January 16, 2023 01:05
-
-
Save sunday4me/8c1dc25e8c7b61ce6ef336de01903591 to your computer and use it in GitHub Desktop.
To access any member of a structure, use the dot operator (.) between the structure variable name and the structure member:
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") | |
| type Person struct { | |
| name string | |
| age int | |
| job string | |
| salary int | |
| } | |
| func main() { | |
| var pers1 Person | |
| var pers2 Person | |
| // Pers1 specification | |
| pers1.name = "Hege" | |
| pers1.age = 45 | |
| pers1.job = "Teacher" | |
| pers1.salary = 6000 | |
| // Pers2 specification | |
| pers2.name = "Cecilie" | |
| pers2.age = 24 | |
| pers2.job = "Marketing" | |
| pers2.salary = 4500 | |
| // Access and print Pers1 info | |
| fmt.Println("Name: ", pers1.name) | |
| fmt.Println("Age: ", pers1.age) | |
| fmt.Println("Job: ", pers1.job) | |
| fmt.Println("Salary: ", pers1.salary) | |
| // Access and print Pers2 info | |
| fmt.Println("Name: ", pers2.name) | |
| fmt.Println("Age: ", pers2.age) | |
| fmt.Println("Job: ", pers2.job) | |
| fmt.Println("Salary: ", pers2.salary) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment