Search⌘ K

Solution Review: Decide Employee Salary

Explore how to define employee structs and implement methods such as giveRaise in Go. Learn to manipulate struct fields to update salaries and print results, helping you master practical struct and method usage.

We'll cover the following...
Go (1.6.2)
package main
import "fmt"
/* basic data structure upon which we'll define methods */
type employee struct {
salary float32
}
/* a method which will add a specified percent to an
employees salary */
func (this *employee) giveRaise(pct float32) {
this.salary += this.salary * pct
}
func main() {
/* create an employee instance */
var e = new(employee)
e.salary = 100000;
/* call our method */
e.giveRaise(0.04)
fmt.Printf("Employee now makes %f", e.salary)
}

In the above code, at line 5, we make a struct employee containing one field salary of type float32. Then, we have an important method giveRaise() ...