V
V
Valery2022-01-17 23:55:18
go
Valery, 2022-01-17 23:55:18

How to force a pointer on an interface-parameter in Go?

type Interface interface {
  Callme() (string, error)
}

func some(upl Interface) {
  upl.Callme()
}


How to "fix" the passing of a pointer to some() in such code?
It is clear that both a suitable structure and a pointer to it will work (implement the interface), but is it possible to somehow force the transfer of the pointer in order to protect yourself from copying the structure when this is not corny required?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
uvelichitel, 2022-01-18
@uvelichitel

Just implement interface only for pointer to struct
func (ms *MyStruct) Callme() (string, error)

K
Kenit, 2022-01-19
@Kenit

If I understand the question correctly, then this is not required, since any interface is essentially already a pointer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question