preface

Due to work requirements, my technology stack has changed all the time, from university to now, language from Java to C# to Node, and now Go. Friends will be false polite you praise: very powerful, will be a lot of language. In fact, only I know, I belong to will not fine series. Therefore, as a P citizen, I still think I should focus on one thing, do in-depth research, do it well, and do it to the utmost. The only good thing is THAT I’ve been doing back-end development. “Language is just a tool.” Yes, but I still think we should specialize in one and expand on the others. No idle talk, today’s topic is mainly to talk about some of my knowledge and understanding of Node and Go.

Node

Node Historical Background

Node creator Ryan Dahl’s primary focus was around high-performance Web servers, and his initial goal was to write an event-driven, non-blocking I/O Web server to achieve higher performance.

JavaScript has a wide range of event-driven applications in browsers, and Chrome’s JavaScript engine V8 has taken the lead in the browser performance wars. This is all part of why JavaScript is the implementation language for Node.

Why is a Node called a Node? The original author called his project web.js, a Web server. Over time, Node has evolved into a single-threaded, single-process system with libraries that share no resources. Each Node process constitutes a Node in the network application, so that’s what the name means.

Characteristics of the Node

Asynchronous I/O

Asynchronous I/O, code that performs an IO operation can proceed without waiting for the result of the request. After a period of time, when the IO returns a result, the CPU is notified to process it. In contrast, synchronization is an I/O operation that needs to wait for completion before continuing.

The classic example is easier to understand:

var fs = require('fs');

fs.readFile('/path'.function(err,files){
    console.log('Reading file completed');
})
console.log('Initiate file reading');

// Output result:
// Initiate a file read
// Reading the file is complete
Copy the code

If reading a file is a long I/O operation, start reading a file is displayed first, and finish reading a file is displayed after reading a file is complete. This is an example of asynchronous I/O. If there are multiple asynchronous I/O operations, the maximum time will be taken. In the case of synchronization, the time spent is the sum of the time spent for multiple operations. This is the advantage of asynchronous I/O.

Events and callback functions

When it comes to events and callbacks, check out Node’s execution model, event loops, and post your own in-depth study notes on this later.

All asynchronous I/O operations in Node send an event to the event queue when they complete. The callback function is executed after each event.

Single thread

Node maintains the single-threaded nature of JavaScript in the browser.

  1. advantage
    1. A single thread does not have to deal with the synchronization of the existence state of multiple threads
    2. Single threads do not have to worry about thread context switching
    3. A single thread also does not cause multithreaded deadlock problems
  2. disadvantage
    1. Unable to utilize multi-core CPU, wasting resources
    2. A single thread error can cause the entire application to quit
    3. Cpu-heavy computing can affect overall performance
cross-platform

Compatible with multiple platforms, mainly based on the underlying Libuv to achieve cross-platform. Libuv builds a platform layer architecture between the operating system and Node

Application scenarios

1, good at I/O intensive application scenarios, mainly through the event cycle mechanism, one thread service all requests, less resource occupation

2. For intensive computing, efficient CPU utilization can be achieved through C/C++ extensions or through Node child processes

3. Distributed applications

Go

Historical Background of Go

Go’s creators wanted to create a new language to replace C++. Go’s execution performance and development efficiency, as well as compilation speed, are among the best, though not the best yet.

Key features of Go

  1. Syntax concise, such as variable declaration, structure declaration, function definition, etc
  2. Static language, static compilation speed, with static language security and performance
  3. Designed for concurrency, Goroutine is a lightweight thread that supports large concurrent processing
  4. Provide a garbage collection mechanism
  5. Common exceptions are processed by returning error objects. Serious exceptions are processed by Panic and RECOVER
  6. Function returns multiple values, easy to accept multiple values
  7. Deferred calls to defer are supported
  8. High memory efficiency
  9. With fewer dependencies, go’s underlying runtime and built-in libraries are very powerful, such as NET/HTTP, sync and Bufio, which are very stable and easy to use. Simple code can build web services
  10. The deployment of convenient
  11. .

A lot of features, and it’s good. This section will be expanded on later in the GO topic

Go Usage Scenarios

  • Server-side development
  • Distributed systems, microservices
  • Network programming
  • Blockchain development
  • Memory KV database, such as boltDB, levelDB

So, if the interviewer asks you what you know about Node and Go, talk about the advantages Node and Go bring and the favorable usage scenarios. Compared to many business scenarios, it can be implemented, so it is applicable, usable, not necessary.

More discoveries can be explored in depth.