GO语言面试系列:(九)golang强制类型转换示例(usafe包)

Chole121 ·
总感觉强制类型转换用这个面试题不太合理。。。。。 ``` func main() { w := worker{job{baseWage: 6000}} printYearMoney(&w) f := farmer{job{baseWage: 4000}} printYearMoney(&f) wa := waiter{job{baseWage: 5000}} printYearMoney(&wa) t := teacher{job: job{6000}, dailyWage: 100} printYearMoney(&t) s := scientist{job: job{20000}, yearWage: 50000} printYearMoney(&s) } type money float64 func (m money) String() string { return fmt.Sprintf("%.2f元", m) } type yearMoney interface { annualSalary() money } func printYearMoney(y yearMoney) { fmt.Println(y, "的年薪为:", y.annualSalary()) } type job struct { baseWage money } func (j *job) annualSalary() money { return j.baseWage * 12 } type worker struct { job } func (w worker) String() string { return "工人" } type farmer struct { job } func (f farmer) String() string { return "农民" } type waiter struct { job } func (w waiter) String() string { return "服务生" } type teacher struct { job dailyWage money } func (t teacher) String() string { return "教师" } func (t *teacher) annualSalary() money { return t.baseWage*12 + t.dailyWage*180 } type scientist struct { job yearWage money } func (s scientist) String() string { return "科学家" } func (s *scientist) annualSalary() money { return s.baseWage*12 + s.yearWage } ```
#1