Why doesn't a specific type satisfy an empty interface in Go?

125 views
Skip to first unread message

Diego L

unread,
Feb 20, 2024, 9:40:02 PMFeb 20
to golang-nuts

I have a simple interface in Go:

Screenshot from 2024-02-20 21-36-15.png

As you can see, params is of type any (interface{})

So, I have the following implementation:

Screenshot from 2024-02-20 21-37-13.png

As you can see, in this case params is of type models.BaseTimeSeriesQuery which is:

Screenshot from 2024-02-20 21-37-50.png

As I understand it, this type must satisfy the empty interface (I understand that any type satisfies it). But later I get this error:

cannot use timeSerieRepositoryInfluxV2 (variable of type *"github.com/diegolatorre16/go-time-series/app/repositories/time_series".influxV2Client) as ports.ReadTimeSeriesRepository value in argument to services.GetTimeSeries: *"github.com/diegolatorre16/go-time-series/app/repositories/time_series".influxV2Client does not implement ports.ReadTimeSeriesRepository (wrong type for method GetTimeSeries) have GetTimeSeries(string, models.BaseTimeSeriesQuery) (*models.TimeSeriesResponse, "github.com/diegolatorre16/go-time-series/app/domain".Error) want GetTimeSeries(string, any) (*models.TimeSeriesResponse, "github.com/diegolatorre16/go-time-series/app/domain".Error)

I'm pretty new to Go, I guess I have some confusion in the way interfaces work in Go


Kurtis Rader

unread,
Feb 20, 2024, 10:10:06 PMFeb 20
to Diego L, golang-nuts
Please resend your question with the screenshots converted to copy/paste of the text. Most of those screenshots are hard to read and we can't copy/paste the text to explore your question.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9b123f84-1b75-44c0-be8e-a62e2a475fcfn%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

burak serdar

unread,
Feb 20, 2024, 10:19:08 PMFeb 20
to Diego L, golang-nuts
This question is already answered on Stack Overflow where you first asked it. To rephrase what is said there: in order to satisfy an interface, the method signature must match exactly. So you must declare the method as GetTimeSeries(string,any) (*models.TimeSeriesResponse,domain.Error)
You can declare the method this way, and then pass a models.BaseTimeSeriesQuery to it, and type assert:

func (c *someClient) GetTimeSeries(database string,params any) (*models.TimeSeriesResponse,domain.Error) {
  query, ok:=params.(models.BaseTimeSeriesQuery)
   if !ok {
      //return error
   }
   // Use query

However, you should consider redesigning this implementation as the use of an interface may not make sense if you need to deal with different data types like this.

Diego L

unread,
Feb 20, 2024, 10:25:11 PMFeb 20
to Kurtis Rader, golang-nuts

👍

Diego L reaccionó a través de Gmail

Diego L

unread,
Feb 20, 2024, 10:25:38 PMFeb 20
to burak serdar, golang-nuts

😀

Diego L reaccionó a través de Gmail

Reply all
Reply to author
Forward
0 new messages