Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting function arguments at runtime in Go

Tags:

How to get function arguments in Go at runtime, all I know is only how to get function name:

pc, file, line, ok := runtime.Caller(2)
rt := runtime.FuncForPC(pc)
return rt.Name() // Foo

What I need is something like this:

Foo(1,2,3)
// Foo_1_2_3
like image 925
Kokizzu Avatar asked Dec 05 '16 04:12

Kokizzu


1 Answers

Not a full answer, but maybe this can help :

package main

import (
    "fmt"
    "reflect"
)

func main() {
    fmt.Println(reflect.TypeOf(f1))
    for index := 0; index < reflect.TypeOf(f1).NumIn(); index++ {
        fmt.Println(reflect.TypeOf(f1).In(index))
    }
}

func f1(a int, b string) {}

prints :

func(int, string) int string

like image 123
R Menke Avatar answered Sep 23 '22 16:09

R Menke