>From my point of view, the main difference is that interfaces are strictly
more powerful, because you can't dynamically type convert a function
type into something different.

You can always make an interface from a function (e.g. http.HandlerFunc
and the like) but not the other way around.

This is a real issue in some places - for example http.Handler is an interface,
but the popular github.com/julienschmidt/httprouter package's Handle
type is a function, which means that you can't use the same router to
(for example) store metadata about routes, because all you've got is
a function.

Another consideration is that a function is only one word, so if you're
likely to be using stateless non-closure functions, and putting them
into interface values, you'll incur less allocations.

On 20 November 2016 at 05:22, Henry <henry.adisuma...@gmail.com> wrote:
> Hi,
>
> I am wondering from best practice point of view whether it is better to use 
> interface or first-class function, especially considering that Go encourages 
> the use of small interfaces. Here is an example:
>
> type SomethingDoer interface{
>    DoSomething(data Data)
> }
>
> func PerformWork(doer SomethingDoer){
>    //...
> }
>
> Or
>
> type SomethingDoer func(Data)
>
> func PerformWork(doer SomethingDoer){
>    //...
> }
>
> Is there some sort of a guideline when to use one vs the other? They seem 
> like redundant features to me.
>
> Thanks
>
> Henry
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to