> Hope there are future plans to extend it so that, > - people can define user function in go and use it in goawk > <https://www.google.com/url?q=https%3A%2F%2Fbenhoyt.com%2Fwritings%2Fgoawk%2F&sa=D&sntz=1&usg=AFQjCNFwoFPJma3opMx-r6FFYqGDxavoNQ> > handling, > as kty... has pointed out. >
I toyed with this, and enabling it simple functions like "math.Abs(float64) float64". You could add math.Abs to a new config.Funcs map and then reflection would figure out the types at runtime and call it. In AWK you'd just do "abs(-5)" and it'd return 5. It's a bit harder with functions that take make complex params and return an error. What would the API look like on the AWK side? Currently AWK knows at parse time what kinds (scalar or array) each parameter is. For example, what about an HTTP request function that took method, url, body and returned status code, headers, and response body. I was thinking for this case you could have the concept of "complex function" which always took two parameters, input args array and output values array. So for http_request it'd look something like: in["method"] = "GET" in["url"] = "https://golang.org/" err = http_request(in, out) if (err != "") { print "network error fetching page:", err exit 1 } if (out["status"] == 404) { print "page not found" exit 1 } print out["body"] It's a bit klunky but at least the API is consistent: functions always take input and output arrays and return an error string (or ""). Anyone here have thoughts on a better AWK-level API? - also enable normal go program to use awk scripts > I'm not sure what you mean here. You can already use GoAWK in normal Go programs against any io.Reader input and io.Writer for output. -Ben -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.