, (, string, int,...) , .
, , . Golang:
, Go , . : , , , , .
, .
, .
type Locker interface {
Lock()
Unlock()
}
Locker
sync
.
, , , Locker. , Locker
, Foo
Bar
Locker
.
type Foo struct {
A string
}
func (f *Foo) String() string {
return f.A
}
func (f *Foo) Lock() {
}
func (f *Foo) Unlock() {
}
type Bar struct {}
func (b *Bar) Lock() {
}
func (b *Bar) Unlock() {
}
, , :
, ( )
:
Locker () , (, Foo, Bar,...).
:
var lock Locker
lock = &Foo{"Foo"}
lock.Lock()
lock.Unlock()
lock.String()
lock = &Bar{}
lock.Lock()
, lock
, , lock
, Locker
. .
?
, . https://softwareengineering.stackexchange.com/questions/108240/why-are-interfaces-useful