TS
GO

TypeScript to Go

10 lessons

Progress0%
1Introduction: Transpiled to Compiled Binary2Type Systems: Structural Interfaces3Functions4Objects to Structs5Generics6Error Handling7Async to Goroutines8Ecosystem9Testing10Go Standard Library
All Mirror Courses
TS
GO
Functions
MirrorLesson 3 of 10
Lesson 3

Functions

Functions

Introduction

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

Mirror Card
TS
From TypeScript:

In TypeScript, you're familiar with functions.

GO
In Go:

Go has its own approach to functions, which we'll explore step by step.

The Go Way

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

GO
Go Example
func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("division by zero")
    }
    return a / b, nil
}

result, err := divide(10, 2)

// Higher-order function
func apply[T, U any](fn func(T) U, value T) U {
    return fn(value)
}

double := func(x float64) float64 { return x * 2 }

Comparing to TypeScript

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

TS
TypeScript (What you know)
function divide(a: number, b: number): [number, Error | null] {
  if (b === 0) return [0, new Error("division by zero")];
  return [a / b, null];
}

const [result, err] = divide(10, 2);

// Higher-order function
function apply<T, U>(fn: (v: T) => U, value: T): U {
  return fn(value);
}

const double = (x: number) => x * 2;
Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

GO
In Go:

Go functions can return multiple values natively — no need for tuples or Result types

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

GO
In Go:

The second return value is conventionally error (nil if no error)

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

GO
In Go:

Go function types: func(T) U instead of TypeScript's (v: T) => U

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

GO
In Go:

Go generics (1.18+) use [T any] instead of <T>

Step-by-Step Breakdown

1. Multiple Return Values

Go natively supports multiple return values. The error-last convention (value, error) replaces TypeScript's Result<T, E> pattern or try/catch.

TS
TypeScript
function findUser(id: number): User | null {
  return db.get(id) ?? null;
}
GO
Go
func findUser(id int) (*User, error) {
    user, ok := db[id]
    if !ok {
        return nil, fmt.Errorf("user %d not found", id)
    }
    return &user, nil
}
Rule of Thumb
Always check the error return value. Ignoring it with _ is a code smell.

2. Function Types

Go function types use func(params) returns syntax. Anonymous functions work like arrow functions.

TS
TypeScript
const greet = (name: string): string => `Hello, ${name}!`;
type Transformer = (s: string) => string;
GO
Go
greet := func(name string) string {
    return "Hello, " + name + "!"
}
type Transformer func(string) string

3. Named Return Values

Go functions can name their return values. This documents intent and allows bare return statements — useful for complex functions.

TS
TypeScript
function minMax(arr: number[]): { min: number; max: number } {}
GO
Go
func minMax(arr []float64) (min, max float64) {
    min, max = arr[0], arr[0]
    for _, v := range arr {
        if v < min { min = v }
        if v > max { max = v }
    }
    return // bare return — returns named values
}

4. defer — Cleanup Functions

defer schedules a function to run when the surrounding function returns. It's the Go way to handle cleanup (close files, release locks) — similar to try/finally.

TS
TypeScript
try {
  const f = openFile();
  process(f);
} finally {
  f.close();
}
GO
Go
f, err := os.Open("file.txt")
if err != nil { return err }
defer f.Close() // runs when function returns

process(f)
Rule of Thumb
Call defer immediately after acquiring a resource so cleanup is never forgotten.

Common Mistakes

When coming from TypeScript, developers often make these mistakes:

  • Go functions can return multiple values natively — no need for tuples or Result types
  • The second return value is conventionally error (nil if no error)
  • Go function types: func(T) U instead of TypeScript's (v: T) => U
Common Pitfall
Don't assume Go works exactly like TypeScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Go supports multiple return values — the (value, error) pattern replaces try/catch
  • Function types use func(T) U syntax
  • Named return values document intent and enable bare returns
  • defer schedules cleanup to run on function exit — replaces try/finally
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your TypeScript code in Go to practice these concepts.
PreviousNext