nodejs pm2 迁移_从NodeJS迁移到Go

nodejs pm2 迁移

The approach of this article is this:

本文的方法是这样的:

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.

您是一位经验丰富的NodeJS开发人员,并且希望学习一种新的语言,但是您不想深入研究,只需了解事物与您当前的专业知识相比,然后做出最终决定即可。

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.

可能是,您最初是一名PHP或Python开发人员,然后找到了进入NodeJS的途径,现在您感觉好像想扩展自己的专业知识。 当然,我可能是错的,但是在本文中,我们将研究使用NodeJS时的一些常见模式以及它们在Go中的比较方式。

为什么去 ( Why Go )

The most common reasons to learn Go include the following:

学习Go的最常见原因包括:

  • Go has only 25 keywords, and this can give you a clue on how simple it is to learn the language. We'll be looking at the common language patterns, and we'll see how cleaner and easier it is to do some common tasks as compared to other languages.

    Go只有25个关键字,这可以为您提供学习该语言有多简单的线索。 我们将研究常见的语言模式,并且将发现与其他语言相比,执行一些常见任务变得更加干净和容易。
  • The Go standard library has a lot of built in functionality, that you will find yourself rarely relying on other external libraries.

    Go标准库具有许多内置功能,您将发现自己很少依赖其他外部库。
  • Go code will not compile if you have unused variables or syntax errors.

    如果您有未使用的变量或语法错误,Go代码将不会编译。
  • Go has great support for concurrency. Other languages support concurrency too, but the approach Go takes is more intuitive, and as easier.

    Go对并发性有很大的支持。 其他语言也支持并发,但是Go所采用的方法更直观,更容易。
  • Go is written in a self documenting way, and so the documentation is quite clear to read and follow along

    Go是通过自我记录方式编写的,因此该文档非常易于阅读和遵循

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!

这是go文档中net / http包的示例。 提供了示例,甚至还有用于源代码文件的选项卡。 甜!

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.

比JavaScript更好吗? 。 不,我不这样认为,也不认为JavaScript更好。 想法是使用正确的工具来完成工作,有时,一种工具比另一种工具更好,或者一个开发人员在一种工具上优于另一种工具。 因此,请学习您能做的,然后选择正确的事情,确定可以为您完成工作。

你好,世界 ( Hello World )

Here's a hello world program in NodeJS

这是NodeJS中的hello world程序

console.log('Hello World');

Running this will produce Hello World in the terminal.

运行此命令将在终端中生成Hello World。

node app.js

Here's an equivalent hello world program in Go

这是Go中等效的hello world程序

package main

import "fmt"

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

Go follows a certain structure which involves:

Go遵循以下特定结构:

  • package name - this usually describes the general idea of what the code will be about. e.g if we were using this to connect to postgres, we may call the package postgres package postgres

    软件包名称 -通常描述代码含义的一般概念。 例如,如果我们使用它连接到postgres,则可以将其称为postgres
  • *import statements - *this where we add in external packages, either from the standard library, or the ones downloaded from the web

    * import声明-*这是我们从标准库或从Web下载的外部软件包中添加外部软件包的地方
  • code - this is the rest of the code, and includes functions, variables, constants and all other language constructs that follow the syntax.

    代码 -这是代码的其余部分,包括函数,变量,常量和遵循该语法的所有其他语言构造。

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

可以通过键入以下内容来运行此代码,或者在GoPlay Space中查看该代码

go run main.go

where main.go is the name of the file.

其中main.go是文件名。

数据类型,变量和常量 ( 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.

JavaScript是动态类型的,这意味着您在定义变量时不必指定类型,并且变量可以在编程时更改其类型。

That being said, JavaScript has the following types:

话虽如此,JavaScript具有以下类型

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

要在JavaScript(NodeJS)中定义变量,我们可以这样编写:

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.

但是,Go是静态类型的,这意味着我们必须事先定义类型,或者分配它们并进行推断。 这是Go Types完整列表

An equivalent of the above JavaScript definitions in Go is

在Go中,上述JavaScript定义的等效项是

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.

在Go中分配初始值是通过使用var关键字,变量名和类型来完成的。 另外,更常见的是,可以使用:=速记语法。 使用:=声明变量时,将自动为它们分配正确的类型。 另外,您可以像在a, b := true, false一样在一行中分配多个值。 这会将ab分别分配给右侧值。

Here is an array of strings in JavaScript

这是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:

尽管Go具有数组 ,但我们通常在JavaScript中将数组称为的是Go中的切片。 这是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

例如,我们将编写一个小程序,该程序返回索引10处的字符串的子字符串。因此,像Luke, I'm not your father这样的句子Luke, I'm not your father会以Luke, I'm结尾Luke, I'm

JavaScript

JavaScript

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

Go

package main

import "fmt"

func main() {
    
    sentence := 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值