go: github.com/shurcooL/go/reflectsource Index | Examples | Files

package reflectsource

import "github.com/shurcooL/go/reflectsource"

Package sourcereflect implements run-time source reflection, allowing a program to look up string representation of objects from the underlying .go source files.

Specifically, it implements ability to get name of caller funcs and their parameters. It also implements functionality to get a string containing source code of provided func.

In order to succeed, it expects the program's source code to be available in normal location. It's intended to be used for development purposes, or for experimental programs.

Code:

var thisIsAFunkyVarName int
var name string = GetExprAsString(thisIsAFunkyVarName)
fmt.Println("Name of var:", name)
fmt.Println("Some func name:", GetExprAsString(strings.HasPrefix))
fmt.Println("Name of second arg:", getMySecondArgExprAsString(5, thisIsAFunkyVarName))

Output:

Name of var: thisIsAFunkyVarName
Some func name: strings.HasPrefix
Name of second arg: thisIsAFunkyVarName

Code:

b := []byte(`this

this is a longer line
and
stuff
last`)

for lineIndex := 0; ; lineIndex++ {
    s, e := getLineStartEndIndicies(b, lineIndex)
    fmt.Printf("%v: [%v, %v]\n", lineIndex, s, e)
    if s == -1 {
        break
    }
}

Output:

0: [0, 4]
1: [5, 5]
2: [6, 27]
3: [28, 31]
4: [32, 37]
5: [38, 42]
6: [-1, -1]

Code:

var f func()

fmt.Println(GetSourceAsString(f))

Output:

nil

Code:

var thisIsAFunkyVarName int
fmt.Println("1 2 3 4:", getMySecondArgExprAsString(1, 2), getMySecondArgExprAsString(3, 4)) // TODO: This should be 2, 4, not 2, 2
fmt.Println("Name of second arg:",                                                          // TODO: This should work
    getMySecondArgExprAsString(5, thisIsAFunkyVarName))

Output:

1 2 3 4: 2 2
Name of second arg: <expr not found>

Code:

f := func(a int, b int) int {
    c := a + b
    return c
}

fmt.Println(GetSourceAsString(f))

Output:

func(a int, b int) int {
	c := a + b
	return c
}

Index

Examples

Package Files

callername.go doc.go funcsource.go indicies.go

func GetExprAsString Uses

func GetExprAsString(_ interface{}) string

GetExprAsString gets the expression as a string.

Code:

var thisIsAFunkyVarName int

fmt.Println("Name of var:", GetExprAsString(thisIsAFunkyVarName))

Output:

Name of var: thisIsAFunkyVarName

func GetFuncValueSourceAsString Uses

func GetFuncValueSourceAsString(fv reflect.Value) string

GetFuncValueSourceAsString returns the source of the func value fv.

Code:

f := func(a int, b int) int {
    c := a + b
    return c
}

fv := reflect.ValueOf(f)

fmt.Println(GetFuncValueSourceAsString(fv))

Output:

func(a int, b int) int {
	c := a + b
	return c
}

func GetParentArgExprAllAsString Uses

func GetParentArgExprAllAsString() []string

GetParentArgExprAllAsString gets all argument expressions of parent func call as a string.

func GetParentArgExprAsString Uses

func GetParentArgExprAsString(argIndex uint32) string

GetParentArgExprAsString gets the argIndex argument expression of parent func call as a string.

func GetParentFuncArgsAsString Uses

func GetParentFuncArgsAsString(args ...interface{}) string

GetParentFuncArgsAsString gets the parent func with its args as a string.

func GetParentFuncAsString Uses

func GetParentFuncAsString() string

GetParentFuncAsString gets the parent func as a string.

func GetSourceAsString Uses

func GetSourceAsString(f interface{}) string

GetSourceAsString returns the source of the func f.

Code:

var f func()
f1 := func() {
    panic(123)
}
f2 := func() {
    println("Hello from anon func!") // Comments are currently not preserved.
}
if 5*5 > 30 {
    f = f1
} else {
    f = f2
}

fmt.Println(GetSourceAsString(f))

Output:

func() {
	println("Hello from anon func!")
}

Package reflectsource imports 12 packages (graph) and is imported by 3 packages. Updated 2016-07-21. Refresh now. Tools for package owners.