Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert function type in Golang

Tags:

go

// Each type have Error() string method.
// The error built-in interface type is the conventional interface for
// representing an error condition, with the nil value representing no error.
//  type error interface {
//   Error() string
//  }

func (f binFunc) Error() string {
    return "binFunc error"
}

func func_type_convert() {
    var err error
    err = binFunc(add)
    fmt.Println(err)
    fmt.Println(i)
}

I have two questions about the code above:

  • I don't know why the Error method executed, when add function was converted into binFunc type?
  • Why the add function converted result was able to assign to an err error interface variable?
like image 758
Jason Zhou Avatar asked Dec 24 '22 14:12

Jason Zhou


1 Answers

error is an interface:

type error interface {
    Error() string
}

This means that any type which has a method: Error() string fulfills the interface and can be assigned to a variable of type error.

binFunc has such a method:

func (f binFunc) Error() string {
    return "binFunc error"
}

New developers in Go sometimes find this confusing because they don't realize it's possible to attach methods to more than just structs. In this case binFunc is defined liked this:

type binFunc func(int, int) int

So the way this works is you are allowed to convert any function which has the same signature: (from the spec)

A function type denotes the set of all functions with the same parameter and result types.

So if you create a function add:

func add(x, y int) int {
    return x + y
}

You are allowed to convert this into a binFunc:

binFunc(add)

And because of the Error method on binFunc we defined above, we are then able to assign this new binFunc to a variable of type error:

var err error
var bf binFunc = binFunc(add)
err = bf

fmt.Println's behavior is to call .Error() on errors for you:

  1. If an operand implements the error interface, the Error method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

So to answer your questions:

  • The Error method is executed because fmt.Println looks for arguments of type error, invokes .Error() and prints the resulting string.
  • You are allowed to assign binFuncs to err because binFunc has an Error method. You cannot assign add directly to err because it does not have an Error method. But you are allowed to convert add to a binFunc because they have the same function signature, and by doing so you can then assign it to the err variable.
like image 90
Caleb Avatar answered Jan 08 '23 19:01

Caleb