[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-go-restful-apis-all--*":3,"academy-blog-translations-jbwlhs3wpmerjdx":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 and RESTful APIs - Create Easy APIs to Connect the World of Data!","sclblg987654321","school_blog_translations","\u003Cp>\u003Cspan style=\"font-size:20px;\">\u003Cstrong>Go and RESTful APIs - Create Easy APIs to Connect the World of Data!\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>Do you want your program to send and receive data with other applications? Today, let's learn how to create RESTful APIs with Go that will help you easily retrieve data (GET) and send data (POST)!\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>What are HTTP and RESTful APIs?\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>HTTP (Hypertext Transfer Protocol) is the protocol used for communication between servers and clients, such as web browsers.\u003Cbr>A RESTful API is a method of communication that uses HTTP to receive and send data between programs, with a simple structure such as requesting data (GET) and sending data (POST).\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>Creating an Easy HTTP Server in Go\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>In Go, we have the net\u002Fhttp package that makes it easy to create an HTTP Server.\u003Cbr>Example Code:\u003Cbr>Code Explanation:\u003Cbr>helloHandler is a function that will display the message \"Hello, World!\" when a request is made to the server's root page.\u003Cbr>http.ListenAndServe(\":8080\", nil) is the command that runs the HTTP Server on port 8080.\u003Cbr>When you run the code and open your browser to http:\u002F\u002Flocalhost:8080, you will see the message \"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>Creating a RESTful API with Go - Receiving Data with GET and POST\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>\u003Cstrong>1. Creating a GET API\u003C\u002Fstrong>\u003Cbr>A GET API is used to retrieve data, such as viewing product details, user information, or general data.\u003Cbr>Example Code:\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>When you visit http:\u002F\u002Flocalhost:8080\u002Fproduct, you will receive product data in JSON format like this:\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. Creating a POST API\u003C\u002Fstrong>\u003Cbr>A POST API is used to send data, such as registering a user or adding new information.\u003Cbr>Example Code:\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>When we send data via POST to http:\u002F\u002Flocalhost:8080\u002Fuser with JSON format data:\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>The result will be a confirmation message for creating a user, such as:\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>Summary\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cul>\u003Cli>GET is used to retrieve data, such as viewing product information, user details, etc.\u003C\u002Fli>\u003Cli>POST is used to send data, such as user registration, adding information, etc.\u003C\u002Fli>\u003Cli>Creating APIs in Go uses the net\u002Fhttp package and JSON for data transmission.\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>You can try creating an API to add new product information using POST and create an API to retrieve product data using GET!\u003C\u002Fp>","32_11zon_rx64x3a1gg.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fbm6fo90dpbmv0pi\u002F32_11zon_rx64x3a1gg.webp","2026-03-04 08:51:47.423Z","",{"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:45.822Z","zzdwleohwkvrpoe","API development","2026-04-10 16:14:39.550Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:20:33.316Z","ln1ntwattzmxo0o","programming","2026-04-10 16:07:27.299Z",{"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},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"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","jbwlhs3wpmerjdx",205,"bm6fo90dpbmv0pi",[20,25,30,35,40,45,50,55],"2025-01-27 04:36:07.109Z","Learn how to create an HTTP Server and RESTful API in Go, along with using JSON for efficient data transmission.","go-restful-apis","2026-04-22 07:11:49.766Z",{"en":92}]