Foundation ⏱ 8 min read

01 · Go for JS Developers

Mental model shift from JavaScript / Node.js to Go

Node.js background? This module maps Go concepts directly to JavaScript equivalents. You will recognize the problems Go solves — just the solutions look different.

Static Typing

Go checks types at compile time. No runtime surprises from unexpected types.

JavaScript
let name = "Alice";
let age = 30;
age = "thirty"; // valid, no error
Go
name := "Alice"  // string inferred
age := 30        // int inferred
// age = "thirty" → compile error

The := operator infers the type once. After that, the type is fixed. Use var x int when you want to declare without assigning.

Zero Values — No undefined

Every variable in Go has a default value. There is no undefined.

var i int     // 0
var s string  // ""
var b bool    // false
var p *int    // nil  (pointers, slices, maps, interfaces can be nil)
Node.js analogy: Like TypeScript with strict null checks on — but zero values are built into the language, not a compiler option.

Multiple Return Values

Go functions can return more than one value. The convention is (result, error).

JavaScript (throw/catch)
function divide(a, b) {
  if (b === 0) throw new Error("div by zero");
  return a / b;
}

try {
  const result = divide(10, 0);
} catch (err) {
  console.error(err.message);
}
Go (return error)
func divide(a, b float64) (float64, error) {
  if b == 0 {
    return 0, errors.New("div by zero")
  }
  return a / b, nil
}

result, err := divide(10, 0)
if err != nil {
  fmt.Println(err)
}

The caller decides what to do with the error. Nothing is thrown — you must check err != nil explicitly. See lesson 03 for the full error handling pattern.

Packages — Not require()

Go code is organized in packages. Import them with their module path.

Node.js
const fs = require('fs');
const express = require('express');
// package.json tracks deps
Go
import (
  "os"
  "github.com/gofiber/fiber/v2"
)
// go.mod tracks deps (like package.json)
// go.sum locks versions (like package-lock.json)

No Classes — Structs + Methods

Go has no class keyword. Use a struct to group data, then attach methods to it.

JavaScript
class User {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  greet() {
    return `Hi, I'm ${this.name}`;
  }
}
Go
type User struct {
  Name string
  Age  int
}

func (u User) Greet() string {
  return "Hi, I'm " + u.Name
}

// Usage:
u := User{Name: "Alice", Age: 30}
fmt.Println(u.Greet())

No inheritance. Use composition and interfaces instead (covered in lesson 02).

Key Takeaways