panic: "reflect.Set: value of type struct {} is not assignable to type error" for explicit type conversion
Author: tom93Created Apr 2, 2026Updated Apr 2, 2026
The following program sample.go triggers an unexpected result
package main
type X struct{}
func (X) Error() string { return "hi" }
func main() {
x := X{}
var e1 error = x // succeeds
println(e1)
var e2 error = error(x) // fails
println(e2)
}Expected result
$ go run ./sample.go
(0x498008,0x544400)
(0x498008,0x544400)Got
$ yaegi ./sample.go
hi
sample.go:7:7: panic: main.main(...)
run: reflect.Set: value of type struct {} is not assignable to type error
goroutine 1 [running]:
runtime/debug.Stack()
/home/user/gox/src/runtime/debug/stack.go:26 +0x5e
github.com/traefik/yaegi/interp.(*Interpreter).Execute.func1()
/home/user/go/pkg/mod/github.com/traefik/[email protected]/interp/program.go:146 +0x79
panic({0xe06500?, 0x89c2a1726c0?})
/home/user/gox/src/runtime/panic.go:860 +0x13a
github.com/traefik/yaegi/interp.runCfg.func1()
/home/user/go/pkg/mod/github.com/traefik/[email protected]/interp/run.go:226 +0x1a5
panic({0xe06500?, 0x89c2a1726c0?})
/home/user/gox/src/runtime/panic.go:860 +0x13a
reflect.Value.assignTo({0xe35fc0?, 0x1793060?, 0x89c2a0418d8?}, {0xf74ee5, 0xb}, 0xe674a0, 0x89c2a1726b0)
/home/user/gox/src/reflect/value.go:3215 +0x288
reflect.Value.Set({0xe674a0?, 0x89c2a1726b0?, 0x0?}, {0xe35fc0?, 0x1793060?, 0x89c2a172600?})
/home/user/gox/src/reflect/value.go:2158 +0xe6
github.com/traefik/yaegi/interp.assign.func3(0x89c29e1c2c0?)
/home/user/go/pkg/mod/github.com/traefik/[email protected]/interp/run.go:722 +0x16d
github.com/traefik/yaegi/interp.runCfg(0x89c2a1763c0, 0x89c29e1c2c0, 0x4?, 0x0?)
/home/user/go/pkg/mod/github.com/traefik/[email protected]/interp/run.go:234 +0x265
github.com/traefik/yaegi/interp.(*Interpreter).run(0x89c29e33448, 0x89c2a175680, 0x89c29e1c210?)
/home/user/go/pkg/mod/github.com/traefik/[email protected]/interp/run.go:119 +0x36d
github.com/traefik/yaegi/interp.(*Interpreter).Execute(0x89c29e33448, 0x89c2a167980)
/home/user/go/pkg/mod/github.com/traefik/[email protected]/interp/program.go:172 +0x225
github.com/traefik/yaegi/interp.(*Interpreter).eval(0x89c29e33448, {0x89c29f16b40?, 0xbb?}, {0x7ffec7bf0937?, 0x89c2a0c5b00?}, 0xc0?)
/home/user/go/pkg/mod/github.com/traefik/[email protected]/interp/interp.go:563 +0x55
github.com/traefik/yaegi/interp.(*Interpreter).EvalPath(0x89c29e33448, {0x7ffec7bf0937, 0x9})
/home/user/go/pkg/mod/github.com/traefik/[email protected]/interp/interp.go:512 +0xa6
main.runFile(0x89c29e33448, {0x7ffec7bf0937, 0x9}, 0x0)
/home/user/go/pkg/mod/github.com/traefik/[email protected]/cmd/yaegi/run.go:153 +0xd8
main.run({0x89c29cfc150, 0x1, 0x1})
/home/user/go/pkg/mod/github.com/traefik/[email protected]/cmd/yaegi/run.go:116 +0x998
main.main()
/home/user/go/pkg/mod/github.com/traefik/[email protected]/cmd/yaegi/yaegi.go:144 +0x2d7Yaegi Version
v0.16.1
Additional Notes
Interestingly, if I replace error with interface{} it succeeds, but the output is
{0x11e22ef16dc0 {0xe35fc0 0x1793060 409}}
{}which is inconsistent (the two lines should be the same).
Source: traefik/yaegi