View : 0

12/04/2026 18:17pm

JS2GO EP.16 Methods and Interfaces in Go vs JavaScript

JS2GO EP.16 Methods and Interfaces in Go vs JavaScript

#JS2GO

#Interfaces JavaScript

#Methods JavaScript

#Interfaces Go

#Methods Go

Methods and Interfaces are fundamental concepts in object-oriented programming and data structure design. Understanding how to create and use Methods and Interfaces allows you to write flexible, readable code and manage functions within structs or objects efficiently.

 

This article compares how Methods and Interfaces work in Go and JavaScript/TypeScript, with code examples, pros and cons, and usage recommendations.

 

Methods in Go

 

In Go, Methods are functions attached to structs. You can use value receivers or pointer receivers to handle struct values.

 

Example of Methods

package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

// Value receiver
func (p Person) Greet() {
    fmt.Printf("Hello, my name is %s and I am %d years old\n", p.Name, p.Age)
}

// Pointer receiver
func (p *Person) HaveBirthday() {
    p.Age += 1
}

func main() {
    person := Person{Name: "Alice", Age: 25}
    person.Greet()        // Hello, my name is Alice and I am 25 years old
    person.HaveBirthday()
    person.Greet()        // Hello, my name is Alice and I am 26 years old
}

 

Pros

  • ✨ Type-safe and clearly bound to structs
  • ✨ Pointer receivers allow modifying struct values

 

Cons

  • ⚠️ Structs must be declared before defining methods
  • ⚠️ No class-based inheritance

 

Interfaces in Go

 

Interfaces define the behavior of structs. Any struct implementing the required methods can be used wherever the interface is expected.

 

Example of Interfaces

package main

import "fmt"

type Greeter interface {
    Greet()
}

type Person struct {
    Name string
}

func (p Person) Greet() {
    fmt.Println("Hello, my name is", p.Name)
}

type Robot struct {
    ID int
}

func (r Robot) Greet() {
    fmt.Println("Beep boop! I am robot", r.ID)
}

func SayHello(g Greeter) {
    g.Greet()
}

func main() {
    alice := Person{Name: "Alice"}
    r2d2 := Robot{ID: 101}
    
    SayHello(alice)  // Hello, my name is Alice
    SayHello(r2d2)   // Beep boop! I am robot 101
}

 

Pros

  • ✨ Decouples code and increases flexibility
  • ✨ Any struct implementing the interface can be used immediately

 

Cons

  • ⚠️ No enforcement of interface implementation
  • ⚠️ Can be confusing for beginners

 

Methods and Objects in JavaScript

 

In JavaScript, Methods are functions inside objects or classes.

 

Example: Methods in Objects

const person = {
    name: "Alice",
    age: 25,
    greet() {
        console.log(`Hello, my name is ${this.name} and I am ${this.age} years old`);
    },
    haveBirthday() {
        this.age += 1;
    }
};

person.greet();       // Hello, my name is Alice and I am 25 years old
person.haveBirthday();
person.greet();       // Hello, my name is Alice and I am 26 years old

 

Example: Methods in Classes

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    greet() {
        console.log(`Hello, my name is ${this.name} and I am ${this.age} years old`);
    }

    haveBirthday() {
        this.age += 1;
    }
}

const alice = new Person("Alice", 25);
alice.greet();
alice.haveBirthday();
alice.greet();

 

Pros

  • ✨ Flexible and easy to use
  • ✨ Supports class-based and prototype-based OOP

 

Cons

  • ⚠️ Not type-safe
  • ⚠️ No built-in interface (requires TypeScript or design patterns)

 

Interface Pattern in TypeScript

 

In JavaScript, interfaces can be implemented using TypeScript:

interface Greeter {
    greet(): void;
}

class Person implements Greeter {
    constructor(public name: string) {}
    greet() {
        console.log(`Hello, my name is ${this.name}`);
    }
}

class Robot implements Greeter {
    constructor(public id: number) {}
    greet() {
        console.log(`Beep boop! I am robot ${this.id}`);
    }
}

function sayHello(g: Greeter) {
    g.greet();
}

const alice = new Person("Alice");
const r2d2 = new Robot(101);

sayHello(alice);  // Hello, my name is Alice
sayHello(r2d2);   // Beep boop! I am robot 101

 

Comparison: Methods and Interfaces

 

FeatureGoJavaScript/TypeScript
Method BindingBound to struct or pointerBound to object or class
InterfaceBuilt-in, decoupled behaviorNo built-in, use pattern/TypeScript
Type SafetyHighLow
InheritanceNo class-based inheritanceSupports class-based inheritance
Use CaseLarge systems, strict typingWeb apps, flexible OOP

 


 

Summary and Recommendations

 

  • Go: Methods and Interfaces are ideal for large systems requiring type safety and behavior flexibility
  • JavaScript/TypeScript: Methods in objects or classes are suitable for web applications and highly flexible code
  • Choose based on project type and needs for function and behavior management

 

Next Episode

 

In EP.17 of the JS2GO series, we will explore debugging in Go and JavaScript to make inspecting and fixing code errors easier and more effective.

 

Read more

🔵 Facebook: Superdev Academy

📸 Instagram: Superdev Academy

🎬 TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH

🌐 Website: https://www.superdevacademy.com/en