Golang中的struct没有构造函数,我们一般可以使用工厂模式来解决这个问题。但是我怎样才能尝试在工厂模式中实现继承呢?例如,这是我的一些代码:
在student.go中
package model
//a struct
type student struct{
Name string
score float64
}
//Because the initial letter of the student structure is lowercase, it can only be used in mods
func NewStudent(n string, s float64) *student {
return &student{
Name : n,
score : s,
}
}
//If the first letter of the score field is lowercase, then, in other packages not directly method, we can provide a method
func (s *student) GetScore() float64{
return s.score //ok
}
Run Code Online (Sandbox Code Playgroud)
在main.go中 …