-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask2.go
53 lines (44 loc) · 1.26 KB
/
task2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Task 2 array of structs
// Here, you can see the two different structs. The company struct uses the employee struct as an array. Write a program that do the following:
// Add three employee using the employee struct (e.g., emp1 := employee{"Amir", 80000, "Full-Stack Developer"}
// Create an array of ‘emplys’ by add the above three records (emplys:=[]employee{ …})
// create a company struct and add values to it (e.g., {"Tetra", emplys})
// Print company details
package main
import (
"fmt"
)
type employee struct {
Name string
Salary int
Role string
}
type company struct {
Name string
Employes []employee
}
func printEmployee(e employee) {
fmt.Println("Name: ", e.Name)
fmt.Println("Salary: ", e.Salary)
fmt.Println("Role: ", e.Role)
fmt.Println()
}
func printCompany(c company) {
fmt.Println("Company Name: ", c.Name)
fmt.Println("Employes: ")
for _, e := range c.Employes {
printEmployee(e)
}
}
func main() {
// Create an array of employees
employees := []employee{
employee{"Amir", 80000, "Full-Stack Developer"},
employee{"Ali", 70000, "Front-End Developer"},
employee{"Ahmed", 60000, "Back-End Developer"},
}
// Create a company struct
company := company{"Tetra", employees}
// Print the company details
printCompany(company)
}