mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 02:16:48 +08:00
update book code
This commit is contained in:
25
eBook/exercises/chapter_10/employee_salary.go
Executable file
25
eBook/exercises/chapter_10/employee_salary.go
Executable file
@@ -0,0 +1,25 @@
|
||||
// methods1.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
/* basic data structure upon with 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)
|
||||
}
|
||||
// Employee now makes 104000.000000
|
Reference in New Issue
Block a user