Go Interface là gì?
Go Interface là gì?
Interface là tập hợp các chữ ký phương thức; kiểu nào có đủ phương thức thì tự động thoả mãn interface (implicit/ structural typing — không cần implements).
type Shape interface {
Area() float64
}
type Rect struct{ W, H float64 }
type Circle struct{ R float64 }
func (r Rect) Area() float64 { return r.W * r.H }
func (c Circle) Area() float64 { return math.Pi * c.R * c.R }
func total(shapes []Shape) (sum float64) {
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.