Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Go have callback concept?

Tags:

I found many talks saying that Node.js is bad because of callback hell and Go is good because of its synchronous model.

What I feel is Go can also do callback as same as Node.js but in a synchronous way. As we can pass anonymous function and do closure things

So, why are they comparing Go and Node.js in callback perspective as if Go cannot become callback hell.

Or I misunderstand the meaning of callback and anonymous function in Go?

like image 949
A-letubby Avatar asked May 17 '14 08:05

A-letubby


People also ask

What is callback in Golang?

If a function is passed as an argument to another function, then such types of functions are known as a Higher-Order function. This passing function as an argument is also known as a callback function or first-class function in the Go language.

What is a callback in Lua?

Callbacks in Lua have the same names and receive the same parameters as callbacks in C, in the same order. In Lua the callbacks they can either return a value or not, the IupLua binding will automatically return IUP_DEFAULT if no value is returned. In Lua callbacks can be Lua functions or strings with Lua code.


1 Answers

A lot of things take time, e.g. waiting on a network socket, a file system read, a system call, etc. Therefore, a lot of languages, or more precisely their standard library, include asynchronous version of their functions (often in addition to the synchronous version), so that your program is able to do something else in the mean-time.

In node.js things are even more extreme. They use a single-threaded event loop and therefore need to ensure that your program never blocks. They have a very well written standard library that is built around the concept of being asynchronous and they use callbacks in order to notify you when something is ready. The code basically looks like this:

doSomething1(arg1, arg2, function() {   doSomething2(arg1, arg2, function() {     doSomething3(function() {       // done     });   }); }); somethingElse(); 

doSomething1 might take a long time to execute (because it needs to read from the network for example), but your program can still execute somethingElse in the mean time. After doSomething1 has been executed, you want to call doSomething2 and doSomething3.

Go on the other hand is based around the concept of goroutines and channels (google for "Communicating Sequential Processes", if you want to learn more about the abstract concept). Goroutines are very cheap (you can have several thousands of them running at the same time) and therefore you can use them everywhere. The same code might look like this in Go:

go func() {   doSomething1(arg1, arg2)   doSomething2(arg1, arg2)   doSomething3()   // done }() somethingElse() 

Whereas node.js focus on providing only asynchronous APIs, Go usually encourages you to write only synchronous APIs (without callbacks or channels). The call to doSomething1 will block the current goroutine and doSomething2 will only be executed after doSomething1 has finished. But that's not a problem in Go, since there are usually other goroutines available that can be scheduled to run on the system thread. In this case, somethingElse is part of another goroutine and can be executed in the meantime, just like in the node.js example.

I personally prefer the Go code, since it's easier to read and reason about. Another advantage of Go is that it also works well with computation heavy tasks. If you start a heavy computation in node.js that doesn't need to wait for network of filesystem calls, this computation basically blocks your event loop. Go's scheduler on the other hand will do its best to dispatch the goroutines on a few number of system threads and the OS might run those threads in parallel if your CPU supports it.

like image 123
tux21b Avatar answered Dec 03 '22 09:12

tux21b