Pjetr
Pjetr

Reputation: 1382

Not enough arguments to a function without arguments

I'm new to GO, so I wanted to try my hand in a little application to help me manage some trips. I'm following the design found here: sohamkamani.com

I'm encountering some errors that don't make sense to me...

src/trip-manager/handlers/PersonHandlers.go:14:40: not enough arguments in call to method expression stores.PersonStore.FindAll
        have ()
        want (stores.PersonStore)

My function doesn't want arguments? What am I missing here?

Also, I didn't encunter this error previously before splitting everything up in packages and before introducing a second store. But because I'm an idiot, I didn't make a commit back then to revert to, to find out what I've done wrong. hopefully someone here might spot my mistake?

handlers/PersonHandlers.go:14

var people, err = PersonStore.FindAll()

stores/PersonStore.go

type PersonStore interface {
  FindAll() ([]*Person, error)
}

type DbPersonStore struct {
  Db *sql.DB
}

var personStore PersonStore

func (personStore *DbPersonStore) FindAll() ([]*Person, error) {
  ...
}

func InitPersonStore(s PersonStore) {
  personStore = s
}

And finally: main.go

func main() {
  fmt.Println("Hello, world. Listening on port 3000")

  router := NewRouter()

  psqlInfo := fmt.Sprintf(
    "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
    cred.Host, cred.Port, cred.User, cred.Password, cred.Database,
  )

  var db, err = sql.Open("postgres", psqlInfo)

  if err != nil {
    panic(err)
  }

  InitPersonStore(&DbPersonStore{Db: db})

  log.Fatal(http.ListenAndServe(":3000", router))
}

Upvotes: 1

Views: 2195

Answers (2)

cn0047
cn0047

Reputation: 17091

The problem with PersonStore.FindAll() is that PersonStore is interface and you just can't run interface method, you have to call struct instance method, like

personStore := DbPersonStore{Db: db}
personStore.FindAll()

Upvotes: 0

RickyA
RickyA

Reputation: 16029

handlers/PersonHandlers.go:14

var people, err = PersonStore.FindAll()

What you are doing is calling FindAll() on the interface instead of on an instance. You probably mistyped for the lowercase

var people, err = personStore.FindAll()

https://play.golang.org/p/nBc6tomURoa

Upvotes: 3

Related Questions