JS
GO

JavaScript to Go

10 lessons

Progress0%
1Variables & Types2Functions3Objects → Structs4Async → Goroutines5Errors & Panic6Interfaces7Slices and Maps8Packages and Modules9Testing10Standard Library
All Mirror Courses
JS
GO
Errors & Panic
MirrorLesson 5 of 10
Lesson 5

Errors & Panic

Error handling in Go

Introduction

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

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with error handling in go.

GO
In Go:

Go has its own approach to error handling in go, 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
package main

import (
    "errors"
    "fmt"
)

var ErrNotFound = errors.New("not found")

func getUser(id int) (*User, error) {
    u, ok := users[id]
    if !ok {
        return nil, fmt.Errorf("user %d: %w", id, ErrNotFound)
    }
    return u, nil
}

u, err := getUser(42)
if err != nil {
    if errors.Is(err, ErrNotFound) {
        fmt.Println("missing")
    } else {
        fmt.Println("error:", err)
    }
}

// Panic — like unrecoverable JS throw
// Use only for programmer errors, not normal flow
defer func() {
    if r := recover(); r != nil {
        fmt.Println("recovered:", r)
    }
}()
panic("something went very wrong")

Comparing to JavaScript

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

JS
JavaScript (What you know)
class NotFoundError extends Error {
  constructor(msg) { super(msg); this.name = "NotFoundError"; }
}

function getUser(id) {
  if (!users.has(id)) throw new NotFoundError("user " + id);
  return users.get(id);
}

try {
  const u = getUser(42);
} catch (err) {
  if (err instanceof NotFoundError) console.log("missing");
  else throw err;
}
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go returns error values; JS throws exceptions

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go errors.Is() checks error identity; JS uses instanceof

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go panic/recover for catastrophic failures; JS throw/catch for everything

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go error wrapping with %w allows errors.Is through the chain

Step-by-Step Breakdown

1. Sentinel Errors

Go package-level error variables (var ErrNotFound = errors.New) replace JS custom error classes for identity comparison.

JS
JavaScript
class NotFoundError extends Error {}
GO
Go
var ErrNotFound = errors.New("not found")

2. errors.Is

errors.Is checks if any error in the chain matches, working through fmt.Errorf %w wrapping.

JS
JavaScript
err instanceof NotFoundError
GO
Go
errors.Is(err, ErrNotFound)

3. Panic vs Throw

panic is for programmer errors (nil pointer, index out of bounds) — not for expected failures. Use error returns for expected failures.

Rule of Thumb
If it can happen in normal operation, return an error. If it represents a bug, panic.

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • Go returns error values; JS throws exceptions
  • Go errors.Is() checks error identity; JS uses instanceof
  • Go panic/recover for catastrophic failures; JS throw/catch for everything
Common Pitfall
Don't assume Go works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Go returns errors; JS throws exceptions
  • Sentinel errors replace custom error classes
  • errors.Is works through error chains
  • panic for bugs; error returns for expected failures
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Go to practice these concepts.
PreviousNext