reflect.Value.Set using unaddressable value

I’m trying to create a struct and attaching fields and methods/functions to it, so I started with:

package main

import (

func main() {
	s := reflect.Zero(reflect.TypeOf(reflect.Struct))
	f := reflect.Zero(reflect.TypeOf(reflect.StructField{
		Name:      "id",
		PkgPath:   "",
		Type:      reflect.TypeOf(reflect.Int),
		Tag:       "",
		Offset:    0,
		Index:     []int{},
		Anonymous: false,

	m := reflect.Zero(reflect.TypeOf(reflect.Method{
		Name:    "add",
		PkgPath: "",
		Type: reflect.TypeOf(reflect.FuncOf(
		Func:  f,
		Index: 0,

	swap := func(in []reflect.Value) []reflect.Value {
		return []reflect.Value{in[1], in[0]}

	t := reflect.ValueOf(&swap).Elem()
	m.Set(reflect.MakeFunc(t.Type(), swap))
	_ = m
	_ = s


But at line m.Set(reflect.MakeFunc(t.Type(), swap)) I got the error:

panic: reflect: reflect.Value.Set using unaddressable value

goroutine 1 [running]:
        D:/Development/go/src/reflect/value.go:262 +0x85
reflect.Value.Set({0xa7ff20?, 0xb53a60?, 0xa8c208?}, {0xa78500?, 0xc00008a210?, 0xa14313?})
        D:/Development/go/src/reflect/value.go:2084 +0x6a
        D:/Deployment/Reflection/add.go:35 +0x3cf
exit status 2

I’ll point you to the docs you should have been able to find. You can’t do that. Initialize m to the value you want it to have.

You should become intimately familiar with this if you plan to use reflection:

Unfortunately, you cannot generate methods on types at runtime, but you can generate structs without methods and then use other reflect functions like MakeFunc to create functions that operate on those structs: Go Playground - The Go Programming Language