The following compilation error makes sense to me
type A int
var bar int
var foo A
bar = 2
foo = bar // cannot use bar (variable of type int) as type A in assignment
, however the following lack of compilation error surprises me
type A func()
var bar func()
var foo A
bar = func() {}
foo = bar
Similarly, here is an example to define and use a type
type A struct{x int}
bar := A{x: 3}
In the same logic I was hoping to define and use a func type with
type A func(a int) string
bar := A{
fmt.Println(a)
return "Hello"
}
but it does not work. One must do
type A func(a int) string
bar := func(a int) string {
fmt.Println(a)
return "Hello"
}
It feels to me that a func
type is a special kind of type. Is it? Wouldn't iot be more correct/intuitive if golang had a type
and a typefunc
keywords, such that
type A func() // would throw a compilation error: a func is not a type
typefunc A func() // would work fine