Khaitan

Khaitan

1565452173

Moving from NodeJS to Go

You are a seasoned NodeJS developer, and are looking to learn a new language, but you don’t want to go deep, just see how things compare with your current expertise, and then make a final decision.

The approach of this article is this:

Table of Contents

  • Why Go
  • Hello World
  • Data Types, Variables and Constants
  • Conditional Statements
  • Loops
  • Objects
  • Http Server
  • Middleware
  • Consuming APIs
  • Database Connections
  • Frameworks
  • Conclusion

You are a seasoned NodeJS developer, and are looking to learn a new language, but you don’t want to go deep, just see how things compare with your current expertise, and then make a final decision.

Chances are, you were initially a PHP or Python developer, then you found your way into NodeJS and now you feel like you want to expand your expertise. Of course I might be wrong, but in this article we are going to look at some common patterns when working with NodeJS and how they compare in Go.

Why Go

The most common reasons to learn Go include the following:

  • Why Go
  • Hello World
  • Data Types, Variables and Constants
  • Conditional Statements
  • Loops
  • Objects
  • Http Server
  • Middleware
  • Consuming APIs
  • Database Connections
  • Frameworks
  • Conclusion

Here’s an example with the net/http package in the go documentation. Examples are provided, and there are even tabs for the source code files. Sweet!

Is Go Better Than **JavaScript **?. No, I don’t think so, nor do I think JavaScript is better. The idea is to use the right tool for the job, and sometimes, one tool is better than another one, or one developer is better in one tool over another. So learn what you can, and choose the right thing that you are sure will get the job done for you.

Hello World

Here’s a hello world program in NodeJS

console.log('Hello World');

Running this will produce Hello World in the terminal.

node app.js

Here’s an equivalent hello world program in Go

package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}

Go follows a certain structure which involves:

  • Why Go
  • Hello World
  • Data Types, Variables and Constants
  • Conditional Statements
  • Loops
  • Objects
  • Http Server
  • Middleware
  • Consuming APIs
  • Database Connections
  • Frameworks
  • Conclusion

This code can be run by typing in the following, or see it in GoPlay Space.

go run main.go

where main.go is the name of the file.

Data Types, Variables and Constants

JavaScript is dynamically typed, which means you do not have to specify types when defining variables, and the variables can change their types as you program along.

That being said, JavaScript has the following types:

To define variables in JavaScript(NodeJS) we’d write this:

const num = 3; // declaring a constant number
let flt = 2.34; // declaring a number (float)
let a = true; // declaring a boolean
let b = false;
var name = 'Scotch'; // declaring a string 

Go however, is statically typed, which means we have to define the types before hand, or assign them and let them be inferred. Here’s a comprehensive list of Go Types.

An equivalent of the above JavaScript definitions in Go is

package main

const num = 3 // declaring a constant

func main() {
    var flt float64 = 2.34 // declaring a float 
    var name string = "Scotch" // declaring a string
    a, b := true, false,  // shorthand declaration syntax
}

Assigning initial values in Go is done by with the var keyword, a variable name, and a type. Additionally and more commonly the := shorthand syntax can be used. When declaring variables with := , they are automatically assigned the correct type. Additionally, you can assign multiple values in one line like we have done in a, b := true, false. This will assign both a and b to the right hand values respectively.

Here is an array of strings in JavaScript

const names = ['Scotch', 'IO'];

While Go has arrays, what we typically refer to arrays in JavaScript are referred to as slices in Go. Here’s an example of a slice in Go:

package main

func main() {
    names := []string{"Scotch", "IO"}
}

As an example, we’ll write a small program that returns the substring of a string at index 10. So a sentence like Luke, I'm not your father will end up being Luke, I'm

JavaScript

const sentence = '`Luke, I\'m not your Father';`
console.log(sentence.substr(0,10));

Go

package main

import "fmt"

func main() {
    sentence := "Luke, I'm not your Father"
    fmt.Println(sentence[:10])
}

You can run the app in Goplay Space

Conditional Statements

Conditional statements include if else and switch statements. Here’s an example in NodeJS.

const age = 10;

if (age > 10) {
    console.log('you are old');
} else {
    console.log('you are young');
}

const gender = 'female';

switch (gender) {
    case 'female':
        console.log('your name is tonie');
        break;
    case 'male':
        console.log('your name is tony');
        break;;
    default:
        ///
}

Here’s an equivalent in Go

package main

import "fmt"

func main() {
    age := 10
    if age > 10 {
        fmt.Println("you are old")
    } else {
        fmt.Println("you are young")
    }

    gender := "female"
    switch gender {
    case "female":
        fmt.Println("your name is tonie ")
    case "male":
        fmt.Println("your name is tony")
    default:
        ///
    }
}

You can run the app in GoPlay Space.

You’ll notice the conditionals are a bit cleaner in Golang, with less brackets.

Loops

JavaScript has 3 loops: for loop, while loop, and a do while loop. Here’s a for loop example.

// normal for loop
for i = 0; i < 10; i++ {
    console.log(i);
}

// key, value loop
for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + ' -> ' + p[key]);
    }
}

// another key value loop
Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
})

// there's also a `for...of`

Go has only one type of loop, and it’s the for loop. Don’t let that deceive you though as the for loop in Go is very versatile and can emulate almost any type of loop. Let’s look at a simple example:

package main

import (
    "fmt"
)

func main() {
    for i := 0; i < 10; i++ {
        fmt.Println(i)
    }

    // key value pairs
    kvs := map[string]string{
        "name":    "Scotch",
        "website": "https://scotch.io",
    }

    for key, value := range kvs {
        fmt.Println(key, value)
    }
}

You can run the app in GoPlay Space.

Objects

Objects are a big part of JavaScript and exist in almost every program. Here’s an Object in JavaScript.

// an object
const Post = {
    ID: 300
    Title: "Moving from NodeJS to Go",
    Author: "Christopher Ganga",
    Difficulty: "Beginner",
}

console.log(Post)

// access values
console.log(Post.ID)
console.log(Post.Title)
console.log(Post.Author)
// ....

// we can also define classes in javascript.

Since Go is statically typed, we need to do a little extra to define objects. There are two ways to do this, and it involves using map. A map is a key value data structure, where the keys are a set (does not contain duplicates).

package main

import (
    "fmt"
)

func main() {
    Post := map[string]interface{}{
        "ID":         300,
        "Title":      "Moving from NodeJS to Go",
        "Author":     "Christopher Ganga",
        "Difficulty": "Beginner",
    }

    fmt.Println(Post)

    // to access values
    fmt.Println(Post["ID"])
    fmt.Println(Post["Title"])
    fmt.Println(Post["Author"])
    // ....
}


You can run this example in Goplay Space

The other way to write obects in Go is by using Structs. A struct is an abstract data structure, with properties and methods. It’s a close equivalent to a Class in Javascript.

package main

import (
    "fmt"
)

type Post struct {
    ID         int
    Title      string
    Author     string
    Difficulty string
}

func main() {
    // create an instance of the Post
    p := Post{
        ID:         300,
        Title:      "Moving from NodeJS to Go",
        Author:     "Christopher Ganga",
        Difficulty: "Beginner",
    }

    fmt.Println(p)

    // to access values
    fmt.Println(p.ID)
    fmt.Println(p.Title)
    fmt.Println(p.Author)
    // ....
}

Struct defines the name of the type and its properties together with the types. We can then create an instance of the type (Post).

You can run this in Goplay Space

Http Server

Now that we know a little about the similarities and differences in language constructs, we can have a look at servers. Since we are coming from NodeJS it’s likely that we’re building a server, that returns JSON for instance.

In NodeJS, chances are while writing a server, you are using Express as the base library for your server. It’s the most common, comes with a router and is the most battle tested. Here’s a NodeJS server.

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello from Express!');
})

app.listen(3000, err => {
  if (err) {
    return console.log('something bad happened', err);
  }

  console.log('`server is listening on 3000'`);
})

The Go standard library provides everything we need to get a server up and running without any external dependencies. The net/http package provides most of this functionality. When building larger applications however, expanding on the base net/http package with third-party packages is common, and one popular package that provides greatly a lot of functionality is the Gorilla Web Toolkit.

Here’s an equivalent Server in Go .

package main

import (
    "net/http"
)

func Hello(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello World"))
}

func main() {
    http.HandleFunc("/", Hello)
    if err := http.ListenAndServe(":8080", nil); err != nil {
        panic(err)
    }
}

We call http.HandleFunc and give it a route and a handler. Almost sililar to the callback we give to express routes.

You can test this by running go run main.go, assuming your file was named main.go

Now, let’s introduce a router library, because, if we don’t, we’ll have to test whether a request came as a POST , GET or the likes, and use if statements to match specific routes. Something like this:

if req.Method == "POST" {
    // do this
}

To get packages with golang, you usually use a go get <github-link>. To get Gorilla Mux from the Gorilla Web Toolkit we mentioned earlier, we would write the following in our terminal:

go get -u github.com/gorilla/mux

Then we are able to do this. I pulled this directly from Gorilla mux documentation.

package main

import (
     "fmt"
     "net/http"

     "github.com/gorilla/mux"
)
func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", HomeHandler)
    r.HandleFunc("/products", ProductsHandler)
    r.HandleFunc("/articles", ArticlesHandler)

    // details
    r.HandleFunc("/products/{key}", ProductHandler)
    r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
    r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)

    http.Handle("/", r)
}

// example handler
func ArticlesCategoryHandler(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    w.WriteHeader(http.StatusOK)
    fmt.Fprintf(w, "Category: %v\n", vars["category"])
}

We see that the same way express accepts patterns, Gorilla mux allows us to use the same patterns. It can get a little complicated than what I’ve described, but I hope you get the idea.

Gorilla mux also comes with helper functions that npm’s body parser helps us achieve. Go purist can however claim that these function can easily be written.

Middleware

Middleware are a great part of NodeJS servers.

They are functions that sit somewhere, and are run before or after the actual request is run. In NodeJS, this is a simple snippet for a middleware that retrieves a secret from the env and uses it to authenticate a user. When reading variables from NodeJS, dotenv is commonly used.

const express = require('express');
const app = express();

// add server_name middleware
function authenticate((req, res, next) => {
    const secret = process.ENV.SECRET;
    if (secret == "") {
        return res.send("secret not found");
    }

    if (!isAuthenticated(req, secret)) {
        return res.send("invalid authentication");
    }

    return next();
})

// use middleware
app.get('/', authenticate, (req, res) => {
  res.send('Hello from Express!');
})

app.listen(3000, err => {
  if (err) {
    return console.log('something bad happened', err);
  }

  console.log('`server is listening on 3000'`);
})

Go takes a similar approach. Since all a middleware does is take in a request, do something with it and decide whether the request should proceed.

package main

import (
    "net/http"
    "os"
)

// our sample authenticate middleware
func Authenticate(next http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r _http.Request) {
        secret := os.Getenv('SECRET')
        if secret == "" {
            w.Write(_[_]byte("secret not found")
            return
        }

        if !isAuthenticated(r, secret) {
            w.Write(_[]byte("invalid authentication"))
            return
        }
        next.ServeHTTP(w, r)
    }
}

func Hello(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello World"))
}

func main() {
    http.HandleFunc("/", Authenticate(Hello)) // call the middeware by wrapping
    if err := http.ListenAndServe(":8080", nil); err != nil {
        panic(err)
    }
}

If you are a seasoned JavaScript developer, you’ve probably noticed functions are first class citizens in Go too.

We’ve just written a function that takes in a [http.HandlerFunc](https://golang.org/pkg/net/http/#HandlerFunc "http.HandlerFunc") which is a function type, and the function structure is type HandlerFunc func(ResponseWriter, *Request), just like the Handler we wrote.

The advantage is that the http.HandlerFunc type has a function ServeHTTP which takes in the response and the request pointer we passed, and executes the handle call.

Some people call this approach wrapping functions, but this is the general idea. Again, you can easily write your own middlewares, but there are a couple of libraries out there to help you like

  • Why Go
  • Hello World
  • Data Types, Variables and Constants
  • Conditional Statements
  • Loops
  • Objects
  • Http Server
  • Middleware
  • Consuming APIs
  • Database Connections
  • Frameworks
  • Conclusion

Consuming APIs

Most of the time, our servers usually depend on an external API to get some data it needs. Let’s say for example we are getting users from Github.

This is the approach you would take in a NodeJS app.

You’ll first install a http request module, such as axios. npm install axios

const axios = require('axios');
const url = 'https://api.github.com/users';

axios.get(url).then(res => {
    // do something with the response
}).catch(err => {
    // do something with the error
})

This piece of code can either be in your service, or anywhere you like.

In Go, however, the [net/http](https://golang.org/pkg/net/http/ "net/http") package can help us with this scenario.

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

func main() {
    URL := "https://api.github.com/users"
    res, err := http.Get(URL)
    if err != nil {
        log.Println(err)
        return
    }
    defer res.Body.Close() // for garbage collection

    responseBodyBytes, err := ioutil.ReadAll(res.Body)
    if err != nil {
        log.Println(err)
        return
    }

    fmt.Println(string(responseBodyBytes))
}

We use http.Get to make requests, and check errors. The response is usually in bytes, and we have to read it first, then convert it to string with string([]bytes).

You can add this to a main.go file and run go run main.go.

This code can also easily be converted to a func, and called repeatedly when needed.

Here’e an example showing various ways to use the http package from the net/http documentation page.

resp, err := http.Get("http://example.com/")
...
resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf)
...
resp, err := http.PostForm("http://example.com/form",
    url.Values{"key": {"Value"}, "id": {"123"}})

Database Connections

NodeJS has various npm modules to help in database connections depending on the databases that you are using.

These libraries most of the time come with their own methods, that try to make it easier for you to work with them, and some even offer ORM like features.

Go however takes a different approach. The standard library provides an interface for working with databases called https://golang.org/pkg/database/sql/ which RDBMS package authors can use to implement drivers for various databases. Authors following the standard interface ensure greater interoperability for their packages.

Common database driver packages include:

  • Why Go
  • Hello World
  • Data Types, Variables and Constants
  • Conditional Statements
  • Loops
  • Objects
  • Http Server
  • Middleware
  • Consuming APIs
  • Database Connections
  • Frameworks
  • Conclusion

Frameworks

We all know how JavaScript has a lot of frameworks and libraries, and we use them ocassionally to avoid reinventing the wheel.

The Go community however prefers using libraries instead of frameworks, so you will rarely find a team commiting to a particular framework when building their applications.

That being said, there is one Framework I’d recommend, since it is built by combining a lot of the commonly used packages, and file structures.

  • Why Go
  • Hello World
  • Data Types, Variables and Constants
  • Conditional Statements
  • Loops
  • Objects
  • Http Server
  • Middleware
  • Consuming APIs
  • Database Connections
  • Frameworks
  • Conclusion

Conclusion

Go’s files must be written within packages, and this usually affects your file structure.

In JavaScript, you’ll see a lot of require statements at the beginning of files, while in Golang, the first line is always a package name, which will then be used as an import path in a file where it’s required import package_path/package_name

I hope you’ve gotten a gist of what it’s like to write Go, and you’d like to get into action. Golang is really praised for it’s concurrency and performance, and if you are building a large application, this would be a preferred choice.

I’ve been following this transitional journey for a while now, and would gladly answer any questions you may have. Just leave a comment.

Thanks for reading

If you liked this post, please do share/like it with all of your programming buddies!

Originally published by Chris Ganga at  scotch.io

Follow us on Facebook | Twitter

Further reading about JavaScript

The Complete JavaScript Course 2019: Build Real Projects!

Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

JavaScript Bootcamp - Build Real World Applications

The Web Developer Bootcamp

JavaScript Programming Tutorial - Full JavaScript Course for Beginners

New ES2019 Features Every JavaScript Developer Should Know

Best JavaScript Frameworks, Libraries and Tools to Use in 2019

What JavaScript Framework You Should Learn to Get a Job in 2019?

Best JavaScript Frameworks, Libraries and Tools to Use in 2019

Microfrontends — Connecting JavaScript frameworks together (React, Angular, Vue etc)

Ember.js vs Vue.js - Which is JavaScript Framework Works Better for You

Do we still need JavaScript frameworks?

#node-js #go #javascript #web-development

What is GEEK

Buddha Community

Moving from NodeJS to Go
Fannie  Zemlak

Fannie Zemlak

1599854400

What's new in the go 1.15

Go announced Go 1.15 version on 11 Aug 2020. Highlighted updates and features include Substantial improvements to the Go linker, Improved allocation for small objects at high core counts, X.509 CommonName deprecation, GOPROXY supports skipping proxies that return errors, New embedded tzdata package, Several Core Library improvements and more.

As Go promise for maintaining backward compatibility. After upgrading to the latest Go 1.15 version, almost all existing Golang applications or programs continue to compile and run as older Golang version.

#go #golang #go 1.15 #go features #go improvement #go package #go new features

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications?

Hire NodeJs Developer to create a real-time, faster, and scalable application to accelerate your business. At HourlyDeveloper.io, we have a team of expert Node.JS developers, who have experience in working with Bootstrap, HTML5, & CSS, and also hold the knowledge of the most advanced frameworks and platforms.

Contact our experts: https://bit.ly/3hUdppS

#hire nodejs developer #nodejs developer #nodejs development company #nodejs development services #nodejs development #nodejs

How to Install NodeJS on Ubuntu 19.04

Overview
In this tutorial, you will learn how to install Node onto Ubuntu 19.04 Disco Dingo. We will cover installation from the default repositories and, for those wanting more recent releases, how to install from the NodeSource repositories.

Installing from Ubuntu
The Ubuntu 19.04 Disco Dingo repository includes NodeJS version 10.15. Like most packages found here, it certainly is not the most recent release; however, if stability is more important than features, it will be your preferred choice.

#nodejs #nodejs 10.x #nodejs 11.x #nodejs 12.x #nodejs 8.x

Top NodeJS Mobile App Development Company in USA

AppClues Infotech is one of the leading NodeJS app development company in USA that offering excellent NodeJS development services for web app development. We provide customized and high-quality NodeJS app development services to clients for different industries with advanced technology and functionalities.

Our dedicated app developers have years of experience in NodeJS development and thus successfully deliver cost-effective and highly customized solutions using the robust JavaScript engine of NodeJS.

Why Choose AppClues Infotech for NodeJS Application Development?
• Fast App Development
• Real-Time Application
• JSON (JavaScript Object Notation) in your Database
• Single Codebase
• Lower Cost
• Built-in NPM Support
• Inexpensive Testing and Hosting

For more info:
Website: https://www.appcluesinfotech.com/
Email: info@appcluesinfotech.com
Call: +1-978-309-9910

#top nodejs app development company in usa #nodejs web app development #nodejs development agency in usa #hire nodejs app developers in usa #custom nodejs app development company #best nodejs app development service company

Arvel  Miller

Arvel Miller

1603068240

Decoding Nodejs

The main goal of this blog is to explain the “Architecture of Nodejs” and to know how the Nodejs works behind the scenes,

Generally, most of the server-side languages, like PHP, ASP.NET, Ruby, and including Nodejs follows multi-threaded architecture. That means for each client-side request initiates a new thread or even a new process.

In Nodejs, all those requests from the clients are handled in a single-thread using shared resources concurrently as It follows the “Single-Threaded Event Loop Model”.

ARCHITECTURE OF NODEJS

What Is EVENT-LOOP?

Event-Loop programming is a flow control in an application-defined by events. The basic principle of Nodejs’s event-driven loop is implementing a central mechanism that hears for events and calls the callback function once an event is turning up.

Nodejs is an event-loop that implements a run-time environment model to achieve non-blocking asynchronous behavior runs on Google Chrome’s V8 engine.

#nodejs #nodejs-developer #nodejs-architecture #nodejs-tutorial #backend #javascript #beginners #event-loop