[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-ep16-methods-interfaces-go-vs-javascript-all--*":3,"academy-blog-translations-jbqj08xq7pk9gcq":79},{"data":4,"page":78,"perPage":78,"totalItems":78,"totalPages":78},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":72,"keywords":73,"locale":49,"published_at":74,"scheduled_at":13,"school_blog":70,"short_description":75,"slug":76,"status":68,"title":6,"updated":77,"updated_by":13,"views":71},"JS2GO EP.16 Methods และ Interfaces ใน Go เทียบกับ JavaScript","sclblg987654321","school_blog_translations","\u003Cp>Methods และ Interfaces เป็นหัวใจสำคัญในการออกแบบโปรแกรมเชิงวัตถุและโครงสร้างข้อมูล การเข้าใจวิธีสร้างและใช้งาน Methods และ Interfaces ช่วยให้โค้ดมีความยืดหยุ่น อ่านง่าย และจัดการฟังก์ชันภายใน struct หรือ object ได้อย่างมีประสิทธิภาพ\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>บทความนี้จะเปรียบเทียบการสร้างและใช้งาน Methods และ Interfaces ใน Go และ JavaScript\u002FTypeScript พร้อมตัวอย่างโค้ด, ข้อดีข้อเสีย, และคำแนะนำการใช้งาน\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Methods ใน Go\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ใน Go, Methods คือฟังก์ชันที่ถูกผูกกับ struct โดยสามารถใช้ value receiver หรือ pointer receiver เพื่อจัดการค่าภายใน struct\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>ตัวอย่าง Methods\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport \"fmt\"\n\ntype Person struct {\n    Name string\n    Age  int\n}\n\n\u002F\u002F Method แบบ value receiver\nfunc (p Person) Greet() {\n    fmt.Printf(\"สวัสดีครับ ผมชื่อ %s และอายุ %d ปี\\n\", p.Name, p.Age)\n}\n\n\u002F\u002F Method แบบ pointer receiver\nfunc (p *Person) HaveBirthday() {\n    p.Age += 1\n}\n\nfunc main() {\n    person := Person{Name: \"Alice\", Age: 25}\n    person.Greet()        \u002F\u002F สวัสดีครับ ผมชื่อ Alice และอายุ 25 ปี\n    person.HaveBirthday()\n    person.Greet()        \u002F\u002F สวัสดีครับ ผมชื่อ Alice และอายุ 26 ปี\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ข้อดี\u003C\u002Fp>\u003Cul>\u003Cli>✨ Type-safe และผูก Method กับ struct ชัดเจน\u003C\u002Fli>\u003Cli>✨ รองรับ pointer receiver เพื่อแก้ไขค่าใน struct\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ข้อเสีย\u003C\u002Fp>\u003Cul>\u003Cli>⚠️ ต้องประกาศ struct ก่อนถึงสร้าง Method ได้\u003C\u002Fli>\u003Cli>⚠️ ไม่มี inheritance แบบ class-based\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Interfaces ใน Go\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Interfaces ใช้สำหรับกำหนด behavior ของ struct ซึ่ง struct ใด ๆ ที่ implement method ตาม interface สามารถใช้ interface ได้ทันที\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>ตัวอย่าง Interface\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport \"fmt\"\n\ntype Greeter interface {\n    Greet()\n}\n\ntype Person struct {\n    Name string\n}\n\nfunc (p Person) Greet() {\n    fmt.Println(\"สวัสดีครับ ผมชื่อ\", p.Name)\n}\n\ntype Robot struct {\n    ID int\n}\n\nfunc (r Robot) Greet() {\n    fmt.Println(\"Beep boop! ผมคือหุ่นยนต์\", r.ID)\n}\n\nfunc SayHello(g Greeter) {\n    g.Greet()\n}\n\nfunc main() {\n    alice := Person{Name: \"Alice\"}\n    r2d2 := Robot{ID: 101}\n    \n    SayHello(alice)  \u002F\u002F สวัสดีครับ ผมชื่อ Alice\n    SayHello(r2d2)   \u002F\u002F Beep boop! ผมคือหุ่นยนต์ 101\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ข้อดี\u003C\u002Fp>\u003Cul>\u003Cli>✨ ช่วยให้โค้ด decoupled และยืดหยุ่น\u003C\u002Fli>\u003Cli>✨ Struct ใด ๆ ที่ implement method ตรงกับ interface สามารถใช้ interface ได้ทันที\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ข้อเสีย\u003C\u002Fp>\u003Cul>\u003Cli>⚠️ ไม่มีการบังคับว่าต้อง implement interface\u003C\u002Fli>\u003Cli>⚠️ อาจสับสนสำหรับผู้เริ่มต้น\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Methods และ Object ใน JavaScript\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ใน JavaScript, Methods คือฟังก์ชันภายใน object หรือ class\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>ตัวอย่าง Methods ใน object\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">const person = {\n    name: \"Alice\",\n    age: 25,\n    greet() {\n        console.log(`สวัสดีครับ ผมชื่อ ${this.name} และอายุ ${this.age} ปี`);\n    },\n    haveBirthday() {\n        this.age += 1;\n    }\n};\n\nperson.greet();       \u002F\u002F สวัสดีครับ ผมชื่อ Alice และอายุ 25 ปี\nperson.haveBirthday();\nperson.greet();       \u002F\u002F สวัสดีครับ ผมชื่อ Alice และอายุ 26 ปี\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>ตัวอย่าง Methods ใน class\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">class Person {\n    constructor(name, age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    greet() {\n        console.log(`สวัสดีครับ ผมชื่อ ${this.name} และอายุ ${this.age} ปี`);\n    }\n\n    haveBirthday() {\n        this.age += 1;\n    }\n}\n\nconst alice = new Person(\"Alice\", 25);\nalice.greet();\nalice.haveBirthday();\nalice.greet();\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ข้อดี\u003C\u002Fp>\u003Cul>\u003Cli>✨ ยืดหยุ่นและใช้งานง่าย\u003C\u002Fli>\u003Cli>✨ รองรับ OOP แบบ class-based และ prototype\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ข้อเสีย\u003C\u002Fp>\u003Cul>\u003Cli>⚠️ ไม่ type-safe\u003C\u002Fli>\u003Cli>⚠️ ไม่มี interface built-in (ต้องใช้ TypeScript หรือ pattern แยกเอง)\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Interface แบบ Pattern ใน JavaScript\u002FTypeScript\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>JavaScript สามารถใช้ TypeScript สร้าง Interface ได้ เช่น:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-typescript\">interface Greeter {\n    greet(): void;\n}\n\nclass Person implements Greeter {\n    constructor(public name: string) {}\n    greet() {\n        console.log(`Hello, my name is ${this.name}`);\n    }\n}\n\nclass Robot implements Greeter {\n    constructor(public id: number) {}\n    greet() {\n        console.log(`Beep boop! I am robot ${this.id}`);\n    }\n}\n\nfunction sayHello(g: Greeter) {\n    g.greet();\n}\n\nconst alice = new Person(\"Alice\");\nconst r2d2 = new Robot(101);\n\nsayHello(alice);  \u002F\u002F Hello, my name is Alice\nsayHello(r2d2);   \u002F\u002F Beep boop! I am robot 101\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>เปรียบเทียบ Methods และ Interfaces\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Feature\u003C\u002Fth>\u003Cth>Go\u003C\u002Fth>\u003Cth>JavaScript\u002FTypeScript\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Method Binding\u003C\u002Ftd>\u003Ctd>ผูกกับ struct หรือ pointer\u003C\u002Ftd>\u003Ctd>ผูกกับ object หรือ class\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Interface\u003C\u002Ftd>\u003Ctd>Built-in, decoupled behavior\u003C\u002Ftd>\u003Ctd>ไม่มี built-in, ต้องใช้ pattern\u002FTS\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Type Safety\u003C\u002Ftd>\u003Ctd>สูง\u003C\u002Ftd>\u003Ctd>ต่ำ\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Inheritance\u003C\u002Ftd>\u003Ctd>ไม่มี class-based inheritance\u003C\u002Ftd>\u003Ctd>รองรับ class-based inheritance\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Use Case\u003C\u002Ftd>\u003Ctd>ระบบขนาดใหญ่, strict typing\u003C\u002Ftd>\u003Ctd>Web apps, flexible OOP\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>สรุปและข้อแนะนำ\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cul>\u003Cli>Go: Methods และ Interfaces เหมาะกับระบบขนาดใหญ่ที่ต้องการความปลอดภัยของ type และความยืดหยุ่นของ behavior\u003C\u002Fli>\u003Cli>JavaScript\u002FTypeScript: Methods ใน object หรือ class เหมาะกับ Web application และโค้ดที่ต้องการความยืดหยุ่นสูง\u003C\u002Fli>\u003Cli>การเลือกใช้ควรพิจารณา ประเภทโปรเจกต์ และ ความต้องการในการจัดการฟังก์ชันและ behavior\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>ตอนต่อไป\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ใน EP.17 ของซีรีส์ JS2GO เราจะพาคุณไปเรียนรู้ การดีบักโค้ดใน Go และ JavaScript เพื่อทำให้การตรวจสอบและแก้ไขข้อผิดพลาดในโค้ดเป็นเรื่องง่ายและมีประสิทธิภาพ\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>อ่านบทความ Series อื่นๆ\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FGolang\">\u003Cstrong>Golang The Series\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FJS2GO\">\u003Cstrong>JS2GO\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FTailwind%20CSS\">\u003Cstrong>10 Ep ที่จะให้คุณเป็นมือโปร Tailwind CSS ในชั่วข้ามคืน\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevschool\u002F\">\u003Cstrong>superdevschool\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdevacademy.com\u002F\">\u003Cstrong>https:\u002F\u002Fwww.superdevacademy.com\u002F\u003C\u002Fstrong>\u003C\u002Fa>\u003Cstrong>&nbsp;\u003C\u002Fstrong>\u003C\u002Fp>","31_11zon_d1h776bm1k.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fufggu3oadyj2ugn\u002F31_11zon_d1h776bm1k.webp","2026-03-04 08:46:42.566Z","",{"keywords":15,"locale":43,"school_blog":53},[16,23,28,33,38],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:46:41.857Z","2fc0mg5t0958mgt","Methods Go","2026-04-10 16:13:17.734Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:46:41.635Z","wy7xha10l98zlz0","Interfaces Go","2026-04-10 16:13:17.641Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:46:41.436Z","gepo3qdsuz9cepe","Methods JavaScript","2026-04-10 16:13:17.577Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:46:41.057Z","0rsso9b0r2shb8t","Interfaces JavaScript","2026-04-10 16:13:17.412Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:45:30.765Z","nayuhpre9emd5xw","JS2GO","2026-04-10 16:12:59.160Z",{"code":44,"collectionId":45,"collectionName":46,"created":47,"flag":48,"id":49,"is_default":50,"label":51,"updated":52},"th","pbc_1989393366","locales","2026-01-22 10:59:55.832Z","twemoji:flag-thailand","s8wri3bt4vgg2ji",true,"Thai","2026-04-10 15:42:46.614Z",{"category":54,"collectionId":55,"collectionName":56,"expand":57,"id":70,"views":71},"hsa1afr8fcnd6qb","pbc_2105096300","school_blogs",{"category":58},{"blogIds":59,"collectionId":60,"collectionName":61,"created":62,"created_by":13,"id":54,"image":63,"image_alt":13,"image_path":64,"label":65,"name":41,"priority":66,"publish_at":67,"scheduled_at":13,"status":68,"updated":69,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:24:37.986Z","js2_go_2_11zon_y6paxmuz32.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fhsa1afr8fcnd6qb\u002Fjs2_go_2_11zon_y6paxmuz32.webp",{"en":41,"th":41},10,"2025-08-11 03:41:08.820Z","published","2026-04-25 02:32:14.339Z","jbqj08xq7pk9gcq",230,"ufggu3oadyj2ugn",[20,25,30,35,40],"2025-09-01 10:59:03.086Z","เรียนรู้การสร้างและใช้งาน Methods และ Interfaces ใน Go และ JavaScript พร้อมตัวอย่างโค้ด ข้อดีข้อเสีย และคำแนะนำการใช้งานในโปรเจกต์จริง","ep16-methods-interfaces-go-vs-javascript","2026-04-25 02:48:07.202Z",1,{"th":76}]