12/04/2026 18:17น.

JS2GO EP.16 Methods และ Interfaces ใน Go เทียบกับ JavaScript
#Methods Go
#Interfaces Go
#Methods JavaScript
#Interfaces JavaScript
#JS2GO
Methods และ Interfaces เป็นหัวใจสำคัญในการออกแบบโปรแกรมเชิงวัตถุและโครงสร้างข้อมูล การเข้าใจวิธีสร้างและใช้งาน Methods และ Interfaces ช่วยให้โค้ดมีความยืดหยุ่น อ่านง่าย และจัดการฟังก์ชันภายใน struct หรือ object ได้อย่างมีประสิทธิภาพ
บทความนี้จะเปรียบเทียบการสร้างและใช้งาน Methods และ Interfaces ใน Go และ JavaScript/TypeScript พร้อมตัวอย่างโค้ด, ข้อดีข้อเสีย, และคำแนะนำการใช้งาน
Methods ใน Go
ใน Go, Methods คือฟังก์ชันที่ถูกผูกกับ struct โดยสามารถใช้ value receiver หรือ pointer receiver เพื่อจัดการค่าภายใน struct
ตัวอย่าง Methods
package main
import "fmt"
type Person struct {
Name string
Age int
}
// Method แบบ value receiver
func (p Person) Greet() {
fmt.Printf("สวัสดีครับ ผมชื่อ %s และอายุ %d ปี\n", p.Name, p.Age)
}
// Method แบบ pointer receiver
func (p *Person) HaveBirthday() {
p.Age += 1
}
func main() {
person := Person{Name: "Alice", Age: 25}
person.Greet() // สวัสดีครับ ผมชื่อ Alice และอายุ 25 ปี
person.HaveBirthday()
person.Greet() // สวัสดีครับ ผมชื่อ Alice และอายุ 26 ปี
}
ข้อดี
- ✨ Type-safe และผูก Method กับ struct ชัดเจน
- ✨ รองรับ pointer receiver เพื่อแก้ไขค่าใน struct
ข้อเสีย
- ⚠️ ต้องประกาศ struct ก่อนถึงสร้าง Method ได้
- ⚠️ ไม่มี inheritance แบบ class-based
Interfaces ใน Go
Interfaces ใช้สำหรับกำหนด behavior ของ struct ซึ่ง struct ใด ๆ ที่ implement method ตาม interface สามารถใช้ interface ได้ทันที
ตัวอย่าง Interface
package main
import "fmt"
type Greeter interface {
Greet()
}
type Person struct {
Name string
}
func (p Person) Greet() {
fmt.Println("สวัสดีครับ ผมชื่อ", p.Name)
}
type Robot struct {
ID int
}
func (r Robot) Greet() {
fmt.Println("Beep boop! ผมคือหุ่นยนต์", r.ID)
}
func SayHello(g Greeter) {
g.Greet()
}
func main() {
alice := Person{Name: "Alice"}
r2d2 := Robot{ID: 101}
SayHello(alice) // สวัสดีครับ ผมชื่อ Alice
SayHello(r2d2) // Beep boop! ผมคือหุ่นยนต์ 101
}
ข้อดี
- ✨ ช่วยให้โค้ด decoupled และยืดหยุ่น
- ✨ Struct ใด ๆ ที่ implement method ตรงกับ interface สามารถใช้ interface ได้ทันที
ข้อเสีย
- ⚠️ ไม่มีการบังคับว่าต้อง implement interface
- ⚠️ อาจสับสนสำหรับผู้เริ่มต้น
Methods และ Object ใน JavaScript
ใน JavaScript, Methods คือฟังก์ชันภายใน object หรือ class
ตัวอย่าง Methods ใน object
const person = {
name: "Alice",
age: 25,
greet() {
console.log(`สวัสดีครับ ผมชื่อ ${this.name} และอายุ ${this.age} ปี`);
},
haveBirthday() {
this.age += 1;
}
};
person.greet(); // สวัสดีครับ ผมชื่อ Alice และอายุ 25 ปี
person.haveBirthday();
person.greet(); // สวัสดีครับ ผมชื่อ Alice และอายุ 26 ปี
ตัวอย่าง Methods ใน class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`สวัสดีครับ ผมชื่อ ${this.name} และอายุ ${this.age} ปี`);
}
haveBirthday() {
this.age += 1;
}
}
const alice = new Person("Alice", 25);
alice.greet();
alice.haveBirthday();
alice.greet();
ข้อดี
- ✨ ยืดหยุ่นและใช้งานง่าย
- ✨ รองรับ OOP แบบ class-based และ prototype
ข้อเสีย
- ⚠️ ไม่ type-safe
- ⚠️ ไม่มี interface built-in (ต้องใช้ TypeScript หรือ pattern แยกเอง)
Interface แบบ Pattern ใน JavaScript/TypeScript
JavaScript สามารถใช้ TypeScript สร้าง Interface ได้ เช่น:
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
เปรียบเทียบ Methods และ Interfaces
| Feature | Go | JavaScript/TypeScript |
|---|---|---|
| Method Binding | ผูกกับ struct หรือ pointer | ผูกกับ object หรือ class |
| Interface | Built-in, decoupled behavior | ไม่มี built-in, ต้องใช้ pattern/TS |
| Type Safety | สูง | ต่ำ |
| Inheritance | ไม่มี class-based inheritance | รองรับ class-based inheritance |
| Use Case | ระบบขนาดใหญ่, strict typing | Web apps, flexible OOP |
สรุปและข้อแนะนำ
- Go: Methods และ Interfaces เหมาะกับระบบขนาดใหญ่ที่ต้องการความปลอดภัยของ type และความยืดหยุ่นของ behavior
- JavaScript/TypeScript: Methods ใน object หรือ class เหมาะกับ Web application และโค้ดที่ต้องการความยืดหยุ่นสูง
- การเลือกใช้ควรพิจารณา ประเภทโปรเจกต์ และ ความต้องการในการจัดการฟังก์ชันและ behavior
ตอนต่อไป
ใน EP.17 ของซีรีส์ JS2GO เราจะพาคุณไปเรียนรู้ การดีบักโค้ดใน Go และ JavaScript เพื่อทำให้การตรวจสอบและแก้ไขข้อผิดพลาดในโค้ดเป็นเรื่องง่ายและมีประสิทธิภาพ
อ่านบทความ Series อื่นๆ
🔵 Facebook: https://www.facebook.com/superdev.academy.th
📸 Instagram: superdevschool
🎬 TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH
🌐 Website: https://www.superdevacademy.com/