Compilation principle

JavaScript is an interpreted or just-in-time compiled language, which generates binary machine code by compilation at runtime. It runs through the following phases (take the V8 engine as an example) :

If you use TypeScript, it compiles to JavaScript code before running it.

The V8 engine first parses the source code to generate an abstract syntax tree (AST), from which the interpreter can start generating bytecode, which the compiler can then generate runnable machine code.

Go is a compiled language that requires the compiler to generate binary machine code before the code can be run. The compilation process looks like this:

The code is scanned (lexical analysis) to generate tokens, followed by an AST generated by Parser, followed by a phase of type checking, usually called semantic analysis, to generate intermediate code, and then optimized to generate target machine code.

The static type

TypeScript and Go are statically typed languages.

For the TypeScript, I often see about its various “SAO” operation, such as from A | B & B:

type UnionToIntersection<U> =
  (U extends any ? (k: U) = >void : never) extends ((k: infer I) = >void)?I : never

interface A {
  name: string;
}

interface B {
  age: number;
}
type Res = UnionToIntersection<A | B>;  // A & B
Copy the code

Or:

type GetArrayMembers<T> = T extends {[index in keyof T]: infer V } ? V : never
const example = [1.2.3] as const;
type Members = GetArrayMembers<typeof example>; / / 1, 2, 3
Copy the code

These “SAO operation”, almost few people can fully understand the meaning of the first glance. It’s even a bit of a “show me my head hurts” kind of thing.

But given that TypeScript is defined as JavaScript’s super, it’s not surprising that TypeScript’s type system is designed to be so complex that JavaScript is always flexible.

In the 1.x version of Go, its static typing is often mocked as “simplicity.” The lack of generics has long been listed as one of the top three problems the language needs to fix, thanks to the usual “I don’t want that” refrain. Fortunately, in version 2.0, the Go language will implement generics.

In addition, the Go language does a nice job of limiting the “when to use interface versus type” question by using a new struct, while interfaces are limited to a collection of abstract methods:

package main;

import "fmt";

type Human struct {
	name string;
	age int;
	phone string;
}

// the function is not the same as the method.
// Human implements SayHi
func (h Human) SayHi(a) {
	fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone);
}


type Men interface {
	SayHi()
}

func main(a) {
	mike := Human{ "mike".25."137xxx"};

	var i Men;

	i = mike;
	i.SayHi();
}
Copy the code

Finally, in TypeScrip you can use any to get around the compiler’s type checking, and in Go you can use an empty interface for similar purposes:

let a: any = 1;
a = 'hello';
Copy the code
var a interface{}
var i int = 5;
s := "Hello world";
a = i;
a = s;
Copy the code

Object orientation and inheritance

JavaScript is a prototype-based inheritance language, and since THE release of ES6, it has been able to use syntactic sugar like class and extends instead of cumbersome writing. TypeScript builds on this by extending the ability of classes to add modifiers such as public, private, and protected.

class AChild {
  protected name = 'hello world'
}

class PA extends AChild {
  say() {
    return this.name; }}Copy the code

In Go, since there are no constructors, when implementing object orientation, you write:

package main;

import "fmt";

type Human struct {
	name string;
	age int;
	phone string;
}

type Employ struct {
	Human; // Anonymous field
	company string;
}

// Human implements SayHi
func (h Human) SayHi(a) {
	fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone);
}

// Human implements the Sing method
func (h Human) Sing(a) {
	fmt.Printf("hahaha");
}

// Employee overloads Human's SayHi method
func (e Employ) SayHi(a) {
	fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name, e.company, e.phone)
}

func main(a) {
	mike := Employ { Human { "mike".25."137xxx" }, "Shanghai" };

	fmt.Printf("name: %s, age: %d. phone: %s, company: %s", mike.name, mike.age, mike.phone, mike.company); // Name: Mike, age: 25, phone: 137xxx
	mike.Sing();    // hahaha
	mike.SayHi();   // Hi, I am Mike, I work at Chihiro. Call me on 137xxx
}
Copy the code

other

  • Don’t worry about whether to use export default or export {XXX} (I always recommend using export {XXX} for some reason). In GO, variables or functions that start with a capital letter are considered exported (public).

  • There are no problems caused by this.

  • Go get XXX package, can get an executable binary file.

  • .


Welcome to the public account