Converting Node.js to Go


(Neel Kumar) #1

Hi,
I have a requirement to convert the source code (node.js) to Go for better Compilation. but I
have completed only the basics in Go. Any help to guide me to start doing it.


(Kristoffer Berdal) #2

Start by learning more Go. I’m assuming you are proficient with Javascript, otherwise I would start by reading about JS before trying to convert it to Go.


(Neel Kumar) #3

Thanks for ur reply
Present I got a task to convert it what any help from ur side from node.js to Go


(Ignacio Gómez) #4

The thing is, what do you need help with? You say you “have completed only the basics in Go”: what do those basics cover? Did you take the Go Tour?

Asking for help to translate code is too broad, we need an actual problem in your translation process to help you.


(Manohar555) #5

if u stuck up with any errors or any doubts i can help u but converting complete code is difficult


(George Calianu) #6

I think that a simple conversion is not a very good idea. I suggest you to rewrite in a idiomatic form the project. One language is dynamic,the other is not and due big differences some kind of conversion can be very ineficient.


(Johan Dahl) #7

This is a joke…

This is the simplest way to convert a node-program into a go-program. When can you tell management you didn’t see any performance advantages of rewriting, execution time and memory usage was about the same :stuck_out_tongue:

package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os/exec"
)

var nodeCode = `
	console.log("Hello from node");
`

func main() {
	// create an temporary file with .js extension
	tempFile, err := ioutil.TempFile("", "*.js")
	if err != nil {
		log.Fatalln(err)
	}
	// write the node code to it
	io.WriteString(tempFile, nodeCode)
	tempFile.Close()

	// run node with file as argument
	cmd := exec.Command("node", tempFile.Name())

	b, err := cmd.Output()
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(string(b))
}