Reflection/Get source: Difference between revisions

Content added Content deleted
(Added Go)
Line 75: Line 75:
<pre>
<pre>
PROC is defined in c:\FreeBasic\getsource.bas at line 3
PROC is defined in c:\FreeBasic\getsource.bas at line 3
</pre>

=={{header|Go}}==
It is possible to get the file name/path and line number of a given function in Go as follows.
<lang go>package main

import (
"fmt"
"path"
"reflect"
"runtime"
)

func someFunc() {
fmt.Println("someFunc called")
}

func main() {
pc := reflect.ValueOf(someFunc).Pointer()
f := runtime.FuncForPC(pc)
name := f.Name()
file, line := f.FileLine(pc)
fmt.Println("Name of function :", name)
fmt.Println("Name of file :", path.Base(file))
fmt.Println("Line number :", line)
}</lang>

{{out}}
<pre>
Name of function : main.someFunc
Name of file : reflection_get_source.go
Line number : 10
</pre>
</pre>