I have written a fairly simple code. It simply “implements” the factory interface in a stateless way. It works as expected, however I got a warning from Golang linter that both make()
and update()
methods are unused, which is clearly not true.
Have I written non-idiomatic Golang code, or is linter just too stupid to understand it? In most other languages this wouldn’t be a problem, but Golang doesn’t allow you to ignore linter warnings.
package main
import "fmt"
type Foo struct {
name string
}
type Factory[T any] interface {
make() *T
update(*T, string, string)
}
type FooFactory struct {
}
func (f *FooFactory) make() *Foo {
return &Foo{}
}
func (f *FooFactory) update(foo *Foo, key string, val string) {
switch key {
case "name":
foo.name = val
}
}
type Service[T any] struct {
factory Factory[T]
}
func (s *Service[T]) Run() {
foo := s.factory.make()
s.factory.update(foo, "name", "qwerty")
fmt.Printf("%v", foo)
}
// prints qwerty
func main() {
service := &Service[Foo]{
factory: &FooFactory{},
}
service.Run()
}