Hi, this is day 9 out of 100 days of code in go.
Today I am playing with struct type. Struct is a go way to define custom types. Use it when standard types doesn’t suite you. Enough words, lets see code examples.
Classic example Employee type
// Employee - new type type Employee struct { ID int Name string Manager *Employee //reference to itself }
ID and Name fields is quite obvious. Manager field is the same type as struct itself, so it must be a reference type and in our business logic will point to Manager data.
Lets create first employee
func main() { // Example of initializing new Employee var worker Employee worker.ID = 1 worker.Name = "Petia Pyatochkin" PrintEmployee(worker) } // PrintEmployee - print data in nice format func PrintEmployee(e Employee) { fmt.Printf("ID = %d\nName = %s\n", e.ID, e.Name) }
Struct fields are accessible using dot notation.
Lets define a manager now and improve print function, so it will print Manger if such exist
func main() { // Example of initializing new struct data var worker Employee worker.ID = 1 worker.Name = "Petia Pyatochkin" PrintEmployee(worker) // Struct can reference on it's own type //Lets define manager for an employee var manager Employee manager.ID = 2 manager.Name = "Middle Level" //using pointer we create a reference to manager struct and keep hierachy of oranization worker.Manager = &manager PrintEmployee(worker) } // PrintEmployee - print data in nice format func PrintEmployee(e Employee) { fmt.Printf("ID = %d\nName = %s\n", e.ID, e.Name) //if e.Manager is defined print manager data if e.Manager != nil { fmt.Printf("Manager of %s:\n", e.Name) //recursively go through managers tree PrintEmployee(*e.Manager) return } fmt.Print("----------\n\n") }
New employee manager is created the same way as first worker variable. When using a reference to new manager variable we assign a manager to worker.
PrintEmployee function has some changes too. First it has new check if Manager reference is not nil and if so using recursion it prints Manager data.
Lets add another Employee, so we will have 3 level management organization
unc main() { // Example of initializing new struct data var worker Employee worker.ID = 1 worker.Name = "Petia Pyatochkin" PrintEmployee(worker) // Struct can reference on it's own type //Lets define manager for an employee var manager Employee manager.ID = 2 manager.Name = "Middle Level" //using pointer we create a reference to manager struct and keep hierachy of oranization worker.Manager = &manager PrintEmployee(worker) //define fields ID and Name using struct literals var cto = Employee{ID: 3, Name: "cto"} manager.Manager = &cto //should print 3 level org structure PrintEmployee(worker) }
Note that new cto variable is using struct literals to define fields values in variable initialization step.
Output of above will be 3 level organization structure:
$ go run struct.go ID = 1 Name = Petia Pyatochkin ---------- ID = 1 Name = Petia Pyatochkin Manager of Petia Pyatochkin: ID = 2 Name = Middle Level ---------- ID = 1 Name = Petia Pyatochkin Manager of Petia Pyatochkin: ID = 2 Name = Middle Level Manager of Middle Level: ID = 3 Name = cto ----------
That’s what you need to know about struct type to use it efficiently in you go programs.
Happy coding!
Source code at Github.