Method can be created for function type but what is best usecase for this?

We can create a type of function as below:

type A func(int, int) int

Also, we can create a method to any type including function type:

func (m myfun) m1() {
	s := m("Ram", "Shyam")

We can call m1 method by on variable of myfun type.

func main() {
	var fn myfun
	fn = func(a, b string) string {
		return fmt.Sprintf("Hi %s and %s, How are you", a, b)

But my question is, What could be the real time example where method to function type will be created?



You will an example here : example
And from the standard lib itself !
I think it’s a good way to aggregate a bunch of functions that need to collaborate without the need of
a shared state (I think it has something to do with fuctional style). It would be better than having an empty struct with methods on it.


  // The HandlerFunc type is an adapter to allow the use of
    // ordinary functions as HTTP handlers. If f is a function
    // with the appropriate signature, HandlerFunc(f) is a
    // Handler that calls f.
    type HandlerFunc func(ResponseWriter, *Request)

    // ServeHTTP calls f(w, r).
    func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
      f(w, r)

It enhances polymorphism, I don’t know any language that has methods defined on functions&types :slight_smile: It is one of my favorites features in go, it is really strange but it is beautiful.


@imatmati and @kync Thank you very much for wonderful answers with stdlib example :slight_smile:

I have written the code which defines methods on function here(inspired by the example shared by @imatmati)

But I am still wondering, why do we need methods on a function. We can achieve the same by defining a function which expects function as a parameter like



If you look at Ali’s example, you’ll have an hint of the usefulness of this possibility.
It can be used to transform a function to an other type, that is to say you can build an adapter. You can find further in the source code an example of use of this technique.

func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
  if handler == nil {
	panic("http: nil handler")
   mux.Handle(pattern, HandlerFunc(handler))

You see how it casts a function to an HandlerFunc. And HandlerFunc is only an adpater that serves to call the function given.

Here a more understandable example I created.

// main
package main

import (

func doTask(input string) {
  	fmt.Println("task executed with input", input)

type TaskAdapter func(string)

func (ta TaskAdapter) job(input string) {

type Task interface {

func run(task Task) {

func main() {

You see here how you can transform a function in a “structure” implementing an interface needed by a function. Have a nice day.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.