[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-go-restful-apis-all--*":3,"academy-blog-translations-1l9zv6de1aqup9t":94},{"data":4,"page":82,"perPage":82,"totalItems":82,"totalPages":82},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":88,"keywords":89,"locale":64,"published_at":90,"scheduled_at":13,"school_blog":86,"short_description":91,"slug":92,"status":84,"title":6,"updated":93,"updated_by":13,"views":87},"EP.14 Go กับ RESTful APIs - สร้าง API ง่ายๆ เชื่อมต่อโลกของข้อมูล!","sclblg987654321","school_blog_translations","\u003Cp>\u003Cspan style=\"font-size:20px;\">\u003Cstrong>Go กับ RESTful APIs - สร้าง API ง่ายๆ เชื่อมต่อโลกของข้อมูล!\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>อยากให้โปรแกรมของคุณรับส่งข้อมูลกับแอปอื่นได้ไหม? วันนี้เรามาเรียนรู้การสร้าง RESTful APIs ด้วย Go ที่จะช่วยให้คุณดึงข้อมูล (GET) และส่งข้อมูล (POST) ได้อย่างง่ายดาย!\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>HTTP และ RESTful API คืออะไร?\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>HTTP (Hypertext Transfer Protocol) คือโปรโตคอลที่ใช้สื่อสารระหว่างเซิร์ฟเวอร์และไคลเอนต์ เช่น เว็บเบราว์เซอร์\u003Cbr>RESTful API คือวิธีการสื่อสารที่ใช้ HTTP ในการรับและส่งข้อมูลระหว่างโปรแกรม โดยมีโครงสร้างที่เข้าใจง่าย เช่น การขอข้อมูล (GET) และการส่งข้อมูล (POST)\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>การสร้าง HTTP Server ง่ายๆ ใน Go\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>ใน Go เรามีแพ็กเกจ net\u002Fhttp ที่ช่วยให้เราสร้าง HTTP Server ได้อย่างง่ายดาย\u003Cbr>ตัวอย่างโค้ด:\u003Cbr>อธิบายโค้ด:\u003Cbr>helloHandler คือฟังก์ชันที่จะแสดงข้อความ \"Hello, World!\" เมื่อมีการร้องขอ (Request) ไปที่หน้าแรกของเซิร์ฟเวอร์\u003Cbr>http.ListenAndServe(\":8080\", nil) คือคำสั่งให้รัน HTTP Server บนพอร์ต 8080\u003Cbr>เมื่อคุณรันโค้ดและเปิดเบราว์เซอร์ไปที่ http:\u002F\u002Flocalhost:8080 คุณจะเห็นข้อความ \"Hello, World!\"\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"net\u002Fhttp\"\r\n)\r\n\r\nfunc helloHandler(w http.ResponseWriter, r *http.Request) {\r\n    fmt.Fprintf(w, \"Hello, World!\")\r\n}\r\n\r\nfunc main() {\r\n    http.HandleFunc(\"\u002F\", helloHandler)\r\n    http.ListenAndServe(\":8080\", nil)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>สร้าง RESTful API ด้วย Go - การรับข้อมูลแบบ GET และ POST\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>\u003Cstrong>1. การสร้าง API แบบ GET\u003C\u002Fstrong>\u003Cbr>API แบบ GET ใช้สำหรับ ดึงข้อมูล เช่น ดูรายละเอียดสินค้า ผู้ใช้งาน หรือข้อมูลทั่วไป\u003Cbr>ตัวอย่างโค้ด:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"encoding\u002Fjson\"\r\n    \"net\u002Fhttp\"\r\n)\r\n\r\ntype Product struct {\r\n    ID    int    `json:\"id\"`\r\n    Name  string `json:\"name\"`\r\n    Price int    `json:\"price\"`\r\n}\r\n\r\nfunc getProductHandler(w http.ResponseWriter, r *http.Request) {\r\n    product := Product{ID: 1, Name: \"Laptop\", Price: 1500}\r\n    json.NewEncoder(w).Encode(product) \u002F\u002F แปลงข้อมูลเป็น JSON แล้วส่งกลับไป\r\n}\r\n\r\nfunc main() {\r\n    http.HandleFunc(\"\u002Fproduct\", getProductHandler)\r\n    http.ListenAndServe(\":8080\", nil)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>เมื่อคุณเรียกดู http:\u002F\u002Flocalhost:8080\u002Fproduct คุณจะได้ข้อมูลของ product ในรูปแบบ JSON ดังนี้:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">{\r\n    \"id\": 1,\r\n    \"name\": \"Laptop\",\r\n    \"price\": 1500\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>2. การสร้าง API แบบ POST\u003C\u002Fstrong>\u003Cbr>API แบบ POST ใช้สำหรับ ส่งข้อมูล เช่น การลงทะเบียนผู้ใช้หรือเพิ่มข้อมูลใหม่\u003Cbr>ตัวอย่างโค้ด:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"encoding\u002Fjson\"\r\n    \"fmt\"\r\n    \"net\u002Fhttp\"\r\n)\r\n\r\ntype User struct {\r\n    Name string `json:\"name\"`\r\n    Age  int    `json:\"age\"`\r\n}\r\n\r\nfunc createUserHandler(w http.ResponseWriter, r *http.Request) {\r\n    var user User\r\n    json.NewDecoder(r.Body).Decode(&amp;user) \u002F\u002F แปลง JSON ที่ได้รับให้เป็น struct User\r\n    fmt.Fprintf(w, \"User created: %s, Age: %d\", user.Name, user.Age)\r\n}\r\n\r\nfunc main() {\r\n    http.HandleFunc(\"\u002Fuser\", createUserHandler)\r\n    http.ListenAndServe(\":8080\", nil)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>เมื่อเราส่งข้อมูลแบบ POST ไปที่ http:\u002F\u002Flocalhost:8080\u002Fuser โดยมีข้อมูลในรูปแบบ JSON:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">{\r\n    \"name\": \"Alice\",\r\n    \"age\": 25\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ผลลัพธ์ที่ได้จะเป็นข้อความยืนยันการสร้างผู้ใช้ เช่น:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">User created: Alice, Age: 25\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>สรุป\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cul>\u003Cli>GET ใช้ดึงข้อมูล เช่น ดูข้อมูลสินค้า ผู้ใช้ ฯลฯ\u003C\u002Fli>\u003Cli>POST ใช้ส่งข้อมูล เช่น การลงทะเบียนผู้ใช้ เพิ่มข้อมูล ฯลฯ\u003C\u002Fli>\u003Cli>การสร้าง API ใน Go ใช้แพ็กเกจ net\u002Fhttp และ JSON สำหรับรับส่งข้อมูล\u003C\u002Fli>\u003C\u002Ful>\u003Cp>สามารถลองสร้าง API สำหรับเพิ่มข้อมูลสินค้าใหม่ด้วยการใช้ POST และสร้าง API ที่ดึงข้อมูลสินค้าด้วย GET ดูได้\u003C\u002Fp>","31_11zon_nt1nekmvku.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F9n5ay8vj7bajmfb\u002F31_11zon_nt1nekmvku.webp","2026-03-04 08:51:47.470Z","",{"keywords":15,"locale":58,"school_blog":68},[16,23,28,33,38,43,48,53],{"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:51:46.116Z","jv51tyfgfy9t9qh","การพัฒนา API","2026-04-10 16:14:39.672Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:31:30.863Z","oyltq82epf0vqka","การเขียนโปรแกรม","2026-04-10 16:07:41.883Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:46:24.109Z","o8xfgwdh6k03hxd","JSON","2026-04-10 16:13:13.679Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:51:46.367Z","n79voie7fvgy5pr","POST","2026-04-10 16:14:39.837Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:51:46.758Z","5jbgvqvjkltkd0x","GET","2026-04-10 16:14:39.910Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:51:47.035Z","jipc9hm4hgin5xd","HTTP Server","2026-04-10 16:14:40.009Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:24:48.510Z","cz98gt1a5wro6em","RESTful API","2026-04-10 16:07:30.300Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"code":59,"collectionId":60,"collectionName":61,"created":62,"flag":63,"id":64,"is_default":65,"label":66,"updated":67},"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":69,"collectionId":70,"collectionName":71,"expand":72,"id":86,"views":87},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":73},{"blogIds":74,"collectionId":75,"collectionName":76,"created":77,"created_by":13,"id":69,"image":78,"image_alt":13,"image_path":79,"label":80,"name":81,"priority":82,"publish_at":83,"scheduled_at":13,"status":84,"updated":85,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":81,"th":81},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","1l9zv6de1aqup9t",322,"9n5ay8vj7bajmfb",[20,25,30,35,40,45,50,55],"2025-01-27 04:36:03.152Z","เรียนรู้วิธีการสร้าง HTTP Server และ RESTful API ในภาษา Go พร้อมการใช้ JSON เพื่อรับและส่งข้อมูลอย่างมีประสิทธิภาพ","go-restful-apis","2026-04-22 07:11:49.630Z",{"th":92}]