Jump to content

Reflection/Get source: Difference between revisions

Added Go
(Added Go)
Line 75:
<pre>
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>
 
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.