GO
TS

Go to TypeScript

10 lessons

Progress0%
1Variables & Types2Functions3Structs & Classes4Interfaces — Both Use Structural Typing5Error Handling6Goroutines vs Async/Await7Slices & Maps → Arrays & Objects8Packages & Modules9Testing10Generics
All Mirror Courses
GO
TS
Packages & Modules
MirrorLesson 8 of 10
Lesson 8

Packages & Modules

Organizing and sharing code

Introduction

In this lesson, you'll learn about packages & modules in TypeScript. Coming from Go, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
GO
From Go:

In Go, you're familiar with organizing and sharing code.

TS
In TypeScript:

TypeScript has its own approach to organizing and sharing code, which we'll explore step by step.

The TypeScript Way

Let's see how TypeScript handles this concept. Here's a typical example:

TS
TypeScript Example
// package.json defines the package
// tsconfig.json configures TypeScript

// mathutil.ts
export function add(a: number, b: number): number {
  return a + b;
}

// Not exported — module-private
function helper(): void {}

// main.ts
import { add } from "./mathutil";
// or: import * as math from "./mathutil";

console.log(add(2, 3));

// External packages via npm
import axios from "axios";

Comparing to Go

Here's how you might have written similar code in Go:

GO
Go (What you know)
// go.mod defines the module
// module github.com/user/myapp
// go 1.21

// mathutil/mathutil.go
package mathutil

// Exported (capital) — public
func Add(a, b int) int { return a + b }

// Unexported (lowercase) — package-private
func helper() {}

// main.go
package main

import (
    "fmt"
    "github.com/user/myapp/mathutil"
)

func main() {
    fmt.Println(mathutil.Add(2, 3))
}
Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go uses go.mod; TypeScript/Node uses package.json + tsconfig.json

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go exports by capitalization; TypeScript uses explicit export keyword

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go imports are package paths; TypeScript imports are file paths or package names

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go has no default exports; TypeScript supports both named and default exports

Step-by-Step Breakdown

1. Export Control

Go uses capitalization to control visibility. TypeScript uses explicit export/import keywords on each symbol.

GO
Go
func Add() {} // exported
func helper() {} // unexported
TS
TypeScript
export function add() {} // exported
function helper() {} // not exported

2. Importing

Go imports full package paths; TypeScript imports relative file paths or installed npm package names.

GO
Go
import "github.com/user/myapp/mathutil"
mathutil.Add(2, 3)
TS
TypeScript
import { add } from "./mathutil";
add(2, 3);

3. Package Manager

Go uses go get and go.mod for dependency management. TypeScript/Node uses npm/yarn/pnpm with package.json.

Rule of Thumb
Run 'go get module@version' vs 'npm install package' — both pin versions in their lockfiles.

Common Mistakes

When coming from Go, developers often make these mistakes:

  • Go uses go.mod; TypeScript/Node uses package.json + tsconfig.json
  • Go exports by capitalization; TypeScript uses explicit export keyword
  • Go imports are package paths; TypeScript imports are file paths or package names
Common Pitfall
Don't assume TypeScript works exactly like Go. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Go exports by capital letter; TypeScript uses export keyword
  • go.mod → package.json + tsconfig.json
  • go get → npm install
  • Both use explicit import statements
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Go code in TypeScript to practice these concepts.
PreviousNext