You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: functions.go
    path of file: ./repos/corteza/extra/server-discovery/vendor/github.com/PaesslerAG/gval
    
    the code of the file until where you have to start completion: package gval

import (
	"context"
	"fmt"
	"reflect"
)

type function func(ctx context.Context, arguments ...interface{}) (interface{}, error)

func toFunc(f interface{}) function {
	if f, ok := f.(func(arguments ...interface{}) (interface{}, error)); ok {
		return function(func(ctx context.Context, arguments ...interface{}) (interface{}, error) {
			var v interface{}
			errCh := make(chan error)
			go func() {
				defer func() {
					if recovere
    
func toFunc(f interface{}) function {
	if f, ok := f.(func(arguments ...interface{}) (interface{}, error)); ok {
		return function(func(ctx context.Context, arguments ...interface{}) (interface{}, error) {
			var v interface{}
			errCh := make(chan error)
			go func() {
				defer func() {
					if recovered := recover(); recovered != nil {
						errCh <- fmt.Errorf("%v", recovered)
					}
				}()
				result, err := f(arguments...)
				v = result
				errCh <- err
			}()

			select {
			case <-ctx.Done():
				return nil, ctx.Err()
			case err := <-errCh:
				return v, err
			}
		})
	}
	if f, ok := f.(func(ctx context.Context, arguments ...interface{}) (interface{}, error)); ok {
		return function(f)
	}

	fun := reflect.ValueOf(f)
	t := fun.Type()
	return func(ctx context.Context, args ...interface{}) (interface{}, error) {
		var v interface{}
		errCh := make(chan error)
		go func() {
			defer func() {
				if recovered := recover(); recovered != nil {
					errCh <- fmt.Errorf("%v", recovered)
				}
			}()
			in, err := createCallArguments(ctx, t, args)
			if err != nil {
				errCh <- err
				return
			}
			out := fun.Call(in)

			r := make([]interface{}, len(out))
			for i, e := range out {
				r[i] = e.Interface()
			}

			err = nil
			errorInterface := reflect.TypeOf((*error)(nil)).Elem()
			if len(r) > 0 && t.Out(len(r)-1).Implements(errorInterface) {
				if r[len(r)-1] != nil {
					err = r[len(r)-1].(error)
				}
				r = r[0 : len(r)-1]
			}

			switch len(r) {
			case 0:
				v = nil
			case 1:
				v = r[0]
			default:
				v = r
			}
			errCh <- err
		}()

		select {
		case <-ctx.Done():
			return nil, ctx.Err()
		case err := <-errCh:
			return v, err
		}
	}
}