Skip to content

Packages, Modules & Imports

TypeScript uses ES modules (import/export) with npm for packages. Go has a similar concept: packages (a directory of .go files sharing a package declaration) and modules (a collection of packages defined by go.mod, like package.json).

Every Go file starts with package <name>. Files in the same directory share a package. Executable programs use package main.

TypeScript
// TypeScript: file is automatically a module
export function add(a: number, b: number): number {
return a + b;
}
export const PI = 3.14159;
Go
// Go: explicit package declaration
package mathutil
func Add(a, b int) int {
return a + b
}
const PI = 3.14159

Go’s export system is elegantly simple: capitalise the first letter → exported (public). lowercase → unexported (private to the package). No export keyword needed.

TypeScript
// TypeScript: explicit export keyword
export function PublicFunc() { ... }
function privateFunc() { ... } // not exported
export class PublicClass { ... }
class InternalClass { ... }
Go
// Go: capitalisation IS the export keyword
func PublicFunc() { ... } // exported — capital P
func privateFunc() { ... } // unexported — lowercase p
type PublicStruct struct { ... }
type internalStruct struct { ... }
TypeScript
// package.json (npm)
{
"name": "my-app",
"version": "1.0.0",
"dependencies": {
"express": "^4.18.0"
}
}
// Add dependency:
// npm install express
Go
// go.mod
module github.com/user/my-app
go 1.22
require (
github.com/gin-gonic/gin v1.9.1
)
// Add dependency:
// go get github.com/gin-gonic/gin
TypeScript
// TypeScript
import { readFileSync } from 'fs';
import express from 'express';
import { add } from './mathutil';
Go
// Go
import (
"os" // stdlib
"github.com/gin-gonic/gin" // third-party
"github.com/user/app/mathutil" // local package
)
package main
import (
"fmt"
"strings"
"unicode"
)
func isExported(name string) bool {
if name == "" {
return false
}
return unicode.IsUpper(rune(name[0]))
}
func main() {
names := []string{"Println", "printf", "HTTP", "myHelper", "PublicAPI"}
for _, n := range names {
status := "unexported"
if isExported(n) {
status = "EXPORTED"
}
fmt.Printf("%-12s -> %s\n", n, status)
}
s := "go for typescript developers"
fmt.Println(strings.ToTitle(s))
}
How does Go determine if an identifier is exported (public)?
What is the Go equivalent of package.json?
Which package declaration must all executable Go programs use?
What command adds a third-party dependency to a Go module?