Skip to content

Pana/from-node-to-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

From Node To Go

If you are familiar with Node.js and want to learn Go, so this is right for you. And same to Goher.

  • Run code
  • Hello world
  • Declare variable
  • For loop
  • Interate
  • if/else
  • function
  • type convert
  • array common operates
  • string common operates
  • send http requests
  • create web server
  • module
  • Get current dir
  • file common operates
  • tools

Run

# node
$ node main.js
# go
$ go run main.go   # in production env you need build and then run

Hello world

console.log("Hello world")
package main

func main() {
    println("Hello world!")
}

data type

Number, String, Boolean, Array, Object, Function, null, undefined
int, float32, byte, string, bool, rune, nil, array, slice, map, struct, interface, chan, func, complex64, uintptr

Declaring variables

var a;
var b = 1;
var c = 'string';
var d = true;
let e = [1, 2, 3];
const F = {hello: 1};
// 直接声明一个变量
var a int
a = 1
// 声明变量并赋初始值
var a int = 1
// 省略类型, 自动推到
var b = 2
// 声明多个变量
var i, b, k int
// 声明多个变量
var width, height = 100, 50
var b, f, s = true, 2.3, "four"
var (
    name   = "naveen"
    age    = 29
    height int
)
// 常量
const freezingF, boilingF = 32.0, 212.0
// 简短声明方式
b := 1
// 使用 _ 表示定义, 而不使用
a, _ = os.Open("./file")
// var 变量名字 类型 = 表达式
// 其中“类型”或“= 表达式”两个部分可以省略其中的一个。如果省略的是类型信息,那么将根据初始化表达式来推导变量的类型信息。如果初始化表达式被省略,那么将用零值初始化该变量

builtin functions

__dirname, setTimeout, setInterval, console, global, process, module, exports
make len cap new append copy close delete complex real imag panic recover

install package

$ npm install package-name  # node
$ go get module-url  # go

import & export

var a = require('fs')
module.exports = function hello() {}
import "os"

// 首字母大写, 自动导出
func Hello() {
}

Current dir

__dirname
os.Getwd()

function

function add (a, b) {
    return a + b;
}

let add = (a, b) => {return a + b;}
func add(a, b int) int {
    return a + b
}
// multiple return value
func openfile (path string) (string, error) {
    return "hello", nil
}
// rest params
func params (a, b int, ...string) int {

}

array

var a = [1, 2, 3];
// array has an specific length
var a [3]int = {1, 2, 3}
// slice's length can change dynamic
var a []int = {1, 2, 3}

object

var a = {key1: 1, key2: 'hello', key3: true, key4: [], key5: {}}
// map's value has same data type
var b map[string]int = {"k1": 1, "k2": 2, "k3": 3}
// struct
type A struct{
    k1 int
    k2 string
    k3 bool
    k4 []int
    k5 struct{}
}
var b A = {1, "hello", false, {}, {}}

if/else

if (condition) {

} else {

}
if condition {

} else if condition {

}

loops

for (initialisation; condition; post) {
}
for (var i = 0; i < 10; i++) {

}
for initialisation; condition; post {
}
for var i = 0; i < 10; i++ {

}
// infinite loop
for {
}

iterate

for (var i in array) {
    console.log(i, array[i]);
}
// ES6
for (var t of iterator) {
    console.log(t)
}
//
for key, val := range mapObject {

}

module

// === ES6
require('path');
// export as whole module
module.exports = function () {
    console.log('hello');
}
// export as part of module
exports.hello = function () {
    console.log('hello');
}
// === ES7
export default const a = 'hello';
import hello from 'hello'
//
export const a = 'hello'
import {hello} from 'hello'
import * as helloModule from 'hello'
package hello  // declare module

func Hello () {  // Name's first letter is uppercase so it is exorted
    println("hello")
}

import "path" // import builtin package
import "golang.org/x/net"  // import third party package

create web server

var http = require("http");
http.createServer(function (request, response) {
	response.writeHead(200, {'Content-Type': 'text/plain'});
	response.end('Hello World\n');
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
package main

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

func main () {
    http.HandleFunc("/", handler) // each request calls handler
    log.Fatal(http.ListenAndServe("localhost:8000", nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello World")
}

About

From Node To Go - Your Cheat Sheet For Node To Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published