01 · Go for JS Developers
Mental model shift from JavaScript / Node.js to Go
Static Typing
Go checks types at compile time. No runtime surprises from unexpected types.
let name = "Alice";
let age = 30;
age = "thirty"; // valid, no error
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)
Multiple Return Values
Go functions can return more than one value. The convention is (result, error).
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);
}
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.
const fs = require('fs');
const express = require('express');
// package.json tracks deps
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.
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hi, I'm ${this.name}`;
}
}
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
- Types are fixed at compile time —
:=infers,var x intdeclares explicitly - No
undefined— every variable has a zero value (0,"",false,nil) - Functions return
(result, error)— always checkerr != nil go.mod=package.json,go.sum=package-lock.json- No classes — structs + methods. No
this— use the receiver variable name (u User)