News for hackers. Stuff that matters.
Re:Why I went from Python to Go (and not node.js) by ricardobeat on Friday September 14, 2012 @05:01PM (#4523933)
Ah, code comparisons. I don't see much difference in the Go code vs Javascript, except for the extra comments and logging in the js. How about this?

    var cluster = require('cluster')
      , http    = require('http')
      , os      = require('os')

    if (cluster.isMaster) {
        os.cpus().forEach(cluster.fork)
    } else {
        http.createServer(function(req, res){
            res.writeHead(200)
            res.end('Hello world')
        }).listen(8000)
    }
Re:Why I went from Python to Go (and not node.js) by enneff on Friday September 14, 2012 @06:17PM (#4524281)
You should update the node.js docs if you consider the original JS snippet ugly, because that's where I pulled the original code sample from verbatim.

In any case, it's still not an apples to apples comparison. The node.js example forks separate processes that share nothing, while in the Go example it all runs in one process so the HTTP handlers can communicate with each other via channels or other shared data structures.

Re:Why I went from Python to Go (and not node.js) by ricardobeat on Friday September 14, 2012 @06:34PM (#4524336)
Shared-nothing is a design decision (and runtime/language limitation), due to the single-threaded nature of js. The workers can communicate with the master via message passing: http://nodejs.org/api/cluster.html#cluster_worker_send_messa...

The documentation has the primary goal of explaining how things work, not being the canonical style guide.

Take a similar example from the Go documentation:

    const NCPU = 4  // number of CPU cores

    func (v Vector) DoAll(u Vector) {
        c := make(chan int, NCPU)  // Buffering optional but sensible.
        for i := 0; i < NCPU; i++ {
            go v.DoSome(i*len(v)/NCPU, (i+1)*len(v)/NCPU, u, c)
        }
        // Drain the channel.
        for i := 0; i < NCPU; i++ {
            <-c    // wait for one task to complete
        }
        // All done.
    }
Re:Why I went from Python to Go (and not node.js) by ilaksh on Friday September 14, 2012 @08:36PM (#4524715)
The idea with node is that you dont normally need multiple threads or processes because you have nonblocking io.

    http = require 'http'
    server = http.createServer 'localhost', (req, res) ->
      res.writeHead 200
      res.end 'ok'
    app = server.listen 3000
Re:Why I went from Python to Go (and not node.js) by ilaksh on Saturday September 15, 2012 @03:36AM (#4525605)
I made a mistake, did not mean to include 'localhost' in there.
Re:Why I went from Python to Go (and not node.js) by exogen on Friday September 14, 2012 @06:36PM (#4524341)
Read again, he didn't say it was ugly. He just pointed out that it wasn't 1:1 because the Go code omitted the logging and comments. A valid criticism.
Re:Why I went from Python to Go (and not node.js) by geekstrada on Friday September 14, 2012 @05:16PM (#4524008)
Thanks, his uneven code comparison was bothering me. The main difference I see is that the nodejs code is easier to read.
Re:Why I went from Python to Go (and not node.js) by enneff on Friday September 14, 2012 @06:18PM (#4524283)
Of course it's easier to read for you: you know JavaScript. I know both JS and Go and the Go version is much easier on my eyes - there's simply a lot less to it.
Re:Why I went from Python to Go (and not node.js) by MatthewPhillips on Friday September 14, 2012 @06:08PM (#4524246)

  os.cpus().forEach(cluster.fork)
Should be:

  os.cpus().forEach(cluster.fork.bind(cluster));
Re:Why I went from Python to Go (and not node.js) by ricardobeat on Friday September 14, 2012 @06:12PM (#4524263)
That's not needed. The API is designed so that you can do

    var fork = require('cluster').fork
Cluster methods keep a reference to the main object inside the module, see https://github.com/joyent/node/blob/master/lib/cluster.js#L4...