JS
GO

JavaScript to Go

10 lessons

Progress0%
1Variables & Types2Functions3Objects → Structs4Async → Goroutines5Errors & Panic6Interfaces7Slices and Maps8Packages and Modules9Testing10Standard Library
All Mirror Courses
JS
GO
Objects → Structs
MirrorLesson 3 of 10
Lesson 3

Objects → Structs

Grouping data and behavior

Introduction

In this lesson, you'll learn about objects → structs 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 grouping data and behavior.

GO
In Go:

Go has its own approach to grouping data and behavior, 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 "fmt"

// Struct (no classes)
type Person struct {
    Name string
    Age  int
}

// Method with receiver
func (p Person) Greet() string {
    return "Hi, I'm " + p.Name
}

// Pointer receiver for mutation
func (p *Person) Birthday() { p.Age++ }

p := Person{Name: "Alice", Age: 30}
fmt.Println(p.Greet())

// Copy and update
updated := p
updated.Age = 31

Comparing to JavaScript

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

JS
JavaScript (What you know)
// Object literal
const person = { name: "Alice", age: 30 };

// Class
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  greet() { return "Hi, I'm " + this.name; }
}

const p = new Person("Alice", 30);
console.log(p.greet());

// Spread / copy
const updated = { ...person, age: 31 };
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go has no classes — structs + methods replace JS classes

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go methods are defined outside the struct with a receiver

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Pointer receiver (*T) required for mutation; JS always mutates by default

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

No class inheritance in Go — use composition (embedding)

Step-by-Step Breakdown

1. Struct vs Class

Go structs define data fields. Methods are separate functions with a receiver parameter — Go's equivalent of class methods.

JS
JavaScript
class Person { constructor(name, age) { this.name=name; } }
GO
Go
type Person struct { Name string; Age int }

2. Receiver Methods

Go attaches behavior to a type via receiver syntax. Use value receiver (p Person) for reads, pointer receiver (*p Person) for writes.

JS
JavaScript
greet() { return "Hi, " + this.name; }
GO
Go
func (p Person) Greet() string { return "Hi, " + p.Name }

3. Composition over Inheritance

Go has no inheritance. Instead, embed one struct in another to reuse its fields and methods.

GO
Go
type Employee struct {
    Person          // embedded — Employee has Name, Age, Greet()
    Department string
}

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • Go has no classes — structs + methods replace JS classes
  • Go methods are defined outside the struct with a receiver
  • Pointer receiver (*T) required for mutation; JS always mutates by default
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 structs replace JS classes
  • Methods use receiver syntax: func (p Person) Method()
  • Pointer receiver for mutation: func (p *Person) Mutate()
  • Composition via embedding replaces inheritance
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