Converting Node.js to Go

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.

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.

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

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.

2 Likes

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

1 Like

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.

2 Likes

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))
}
3 Likes