On Sat, Nov 5, 2022 at 1:58 PM Shubh Karman Singh <sksingh2...@gmail.com> wrote:
> I am refactoring a JS library to Go. I have some questions but I'm not > able to find some concrete answers for this. > 1. What's the idiomatic way to refactor Asynchronous JS APIs to Go? > 2. How to refactor Callbacks from JS to Golang? I have seen some Go code > which takes function as an input parameter. Is the intent same here? > I've been programming for more than four decades. In that time I've personally rewritten three apps in a different language. I've also read several post translation reports for other projects. The most important lesson is that it is usually a mistake to perform a literal translation. In fact, of the rewrites I've participated in or read about a literal translation was appropriate in only one case. That was the first one I ever did: translating Donald Knuth's TeX typesetting program from Pascal to C. I haven't written any Javascript in over a decade. But translating its async API pattern to idiomatic Go is likely to involve starting a goroutine for each async op and having the goroutine communicate a result via a channel. For your second question, yes, functions are first-class objects in Go and can be passed to other functions to perform a specialized operation or act as a callback. -- Kurtis Rader Caretaker of the exceptional canines Junior and Hank -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD_vVxKcvaeg-S2RJwNXs64s_O-tHhJsGBDaX9yB8V8Xyg%40mail.gmail.com.