Skip to content

Instantly share code, notes, and snippets.

@santosh
Created December 14, 2018 18:00
Show Gist options
  • Save santosh/269b56ce455d885dcf90a918662c119a to your computer and use it in GitHub Desktop.
Save santosh/269b56ce455d885dcf90a918662c119a to your computer and use it in GitHub Desktop.
Classes and constructor in #golang.
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))
}
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