-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to Call Go Method/Function (Standard Lib) #22
Comments
Hi Kaveh. There are two mechanisms for reaching out to Go from zygo. The standard library is of course available to import from within Go code, but Go libraries are in general not available without some (light) extra work on the part of the user. That work involves using one of the two extension mechanisms provided by zygo and documented in the wiki: function based extension, or struct based extension. a) adding functions - wrapping native Go function calls Here you would add a wrapper for the Year() call, for example. Here is the documentation and pointers to examples of how time.Now() is implimented as an example. https://github.com/glycerine/zygomys/wiki/Go-API#adding-go-functions https://github.com/glycerine/zygomys/blob/master/zygo/time.go#L32 https://github.com/glycerine/zygomys/blob/master/zygo/time.go#L101 b) wrapping native Go structs This provides a bit more flexibility, as we can call methods on structs that are discovered at runtime. However the structs themselves must be registered and thus compiled in during your use of zygo as an embedded libarary. This is also discussed on the wiki pointer above. Here are pointers to a demo of the struct based extension: https://github.com/glycerine/zygomys/blob/master/zygo/demo_go_structs.go https://github.com/glycerine/zygomys/blob/master/tests/methodcall.zy Lastly, to answer your other questions --
(methodls) will list methods available when using the struct-based extension described above. Here is sample code: https://github.com/glycerine/zygomys/blob/master/tests/methodls.zy Once you have bound your shadow go struct to a zygo SexpHash table, invoking (togo) on the hash table will print the corresponding Go structure.
Does printing the value of the package not give you sufficient information? For example, from the package.zy example in tests/ (https://github.com/glycerine/zygomys/blob/master/tests/package.zy).
|
First of all I should thank you for this great work! And thanks for this thorough answer. Having a bit more descriptive I love to see programming languages (and other tools) in pure Go and there is nothing wrong with having special ways of communication with native Go packages. I suppose Why at the REPL does it show |
:) Thanks for the nice words. The REPL should show
All s-expressions know how to print themselves, so at a minimum (str x) should give some information for any x. Also (pretty true) gives nice pretty printing indentation of nested maps/Go structs. |
also to note, at the |
Last, of note, objects just by printing them are fairly informative. I'll make up a two level object heirarchy here, and then print it flat and pretty.
|
Thanks! Should I close this? Or will it be labeled as |
I think it might be easier for others to find if its left open, so I'll put a question label on it and leave it open for now. |
Question about this: the struct approach for this documented in the wiki points to files no longer found:
I'm trying to figure out how to implement Go interop in Zygomys, so would love to see those files ... or pointers to where that code now lives :-) Thanks! |
Ah, looks like these might have some good info: |
@oubiwann Yes, the package was renamed, from |
How can a method be called? For example if
(def t (now))
, is it possible to call theYear()
method? Also how toimport
standard packages? If we evaluate(import "time")
(REPL) we get this error:Is there an
inspect
function that gives info about the type, fields and methods of an object? Or exported from a package?Edit
I studied the
zygo
command and seems extra functionality should be added via Go API (which is fine). Still I'm interested in some answers to questions above.The text was updated successfully, but these errors were encountered: