Created
December 14, 2018 18:00
-
-
Save santosh/269b56ce455d885dcf90a918662c119a to your computer and use it in GitHub Desktop.
Classes and constructor in #golang.
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 employee | |
import "fmt" | |
// employee represents an employee | |
type employee struct { | |
firstName string | |
lastName string | |
totalLeaves int | |
leavesTaken int | |
} | |
// New construct a new employee | |
func New(firstName string, lastName string, totalLeaves int, leavesTaken int) employee { | |
e := employee{firstName, lastName, totalLeaves, leavesTaken} | |
return e | |
} | |
func (e employee) LeavesRemaining() { | |
fmt.Printf("%s %s has %d leaves remaining\n", e.firstName, e.lastName, (e.totalLeaves - e.leavesTaken)) | |
} |
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 "oop/employee" | |
func main() { | |
e := employee.New("Santosh", "Kumar", 5, 4) | |
e.LeavesRemaining() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment