substituted go class structure

We know the feeling of the structure go a little class can have its own properties and methods.

However, due to the structure of the property is a zero value, we do not need to set the values ​​of these properties when creating the structure will be able to create, but to create such a structure is often little practical value.

We can customize a constructor, and then use this method to construct this structure

package employee

import (  
    "fmt"
)

type employee struct {  
    firstName   string
    lastName    string
    totalLeaves int
    leavesTaken int
}

func New(firstName string, lastName string, totalLeave int, leavesTaken int) employee {  
    e := employee {firstName, lastName, totalLeave, leavesTaken}
    return e
}

func (e employee) LeavesRemaining() {  
    fmt.Printf("%s %s has %d leaves remaining", e.firstName, e.lastName, (e.totalLeaves - e.leavesTaken))
}

 

Guess you like

Origin www.cnblogs.com/tigerzhouv587/p/11497504.html