tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type example struct{}
|
||||
|
||||
// the method must be exported to be accessed through reflection.
|
||||
func (example) Foo() int {
|
||||
return 42
|
||||
}
|
||||
|
||||
func main() {
|
||||
// create an object with a method
|
||||
var e example
|
||||
// get the method by name
|
||||
m := reflect.ValueOf(e).MethodByName("Foo")
|
||||
// call the method with no argments
|
||||
r := m.Call(nil)
|
||||
// interpret first return value as int
|
||||
fmt.Println(r[0].Int()) // => 42
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
procedure main()
|
||||
x := foo() # create object
|
||||
x.m1() # static call of m1 method
|
||||
# two examples where the method string can be dynamically constructed ...
|
||||
"foo_m1"(x) # ... need to know class name and method name to construct name
|
||||
x.__m["m1"] # ... general method (better)
|
||||
end
|
||||
|
||||
class foo(a,b,c) # define object
|
||||
method m1(x)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue