[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-go-pointers-memory-management-all--*":3,"academy-blog-translations-hkixn5qaznhwdjv":79},{"data":4,"page":67,"perPage":67,"totalItems":67,"totalPages":67},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":73,"keywords":74,"locale":49,"published_at":75,"scheduled_at":13,"school_blog":71,"short_description":76,"slug":77,"status":69,"title":6,"updated":78,"updated_by":13,"views":72},"EP.6 Go กับ Pointers - ควบคุมหน่วยความจำได้อยู่หมัด!","sclblg987654321","school_blog_translations","\u003Cp>\u003Cspan style=\"font-size:20px;\">\u003Cstrong>Go กับ Pointers - ควบคุมหน่วยความจำได้อยู่หมัด!\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>เคยสงสัยไหมว่าโปรแกรมจัดการข้อมูลในหน่วยความจำได้อย่างไร? Pointers ใน Go จะช่วยให้คุณเข้าถึงและแก้ไขค่าผ่านที่อยู่หน่วยความจำโดยตรง\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>Pointers คืออะไร?\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>ในภาษา Go, Pointer เป็นตัวแปรที่เก็บ ที่อยู่หน่วยความจำ ของค่าหรือตัวแปรอื่น ช่วยให้เราสามารถเข้าถึงและแก้ไขค่าผ่านหน่วยความจำโดยตรงได้\u003Cbr>ตัวอย่าง:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    x := 10\r\n    ptr := &amp;x \u002F\u002F เก็บที่อยู่ของตัวแปร x\r\n    fmt.Println(\"Address of x:\", ptr) \u002F\u002F แสดงที่อยู่หน่วยความจำ\r\n    fmt.Println(\"Value of x:\", *ptr)  \u002F\u002F ดึงค่า x ผ่าน pointer\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>การใช้ Pointer ในฟังก์ชัน\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>ฟังก์ชันสามารถรับค่าโดยการอ้างอิงผ่าน Pointer ทำให้สามารถแก้ไขค่าของตัวแปรต้นทางได้โดยตรง:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">func changeValue(num *int) {\r\n    *num = 100\r\n}\r\n\r\nfunc main() {\r\n    x := 10\r\n    changeValue(&amp;x)\r\n    fmt.Println(\"Updated value of x:\", x) \u002F\u002F Output: 100\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>การจัดการหน่วยความจำใน Go\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>Go มี Garbage Collector ที่ช่วยจัดการหน่วยความจำโดยอัตโนมัติ แต่การเข้าใจการใช้ Pointer จะช่วยให้คุณเขียนโปรแกรมที่มีประสิทธิภาพและหลีกเลี่ยงการใช้หน่วยความจำเกินความจำเป็น\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>Nil Pointer และการตรวจสอบ Pointer\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>Pointer ที่ไม่ได้ชี้ไปที่ตัวแปรใดๆ จะมีค่าเป็น nil:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">var ptr *int\r\nif ptr == nil {\r\n    fmt.Println(\"Pointer is nil\")\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>สามารถลองสร้างฟังก์ชันที่ใช้ Pointer ในการสลับค่าของตัวแปรสองตัว\u003Cbr>ตัวอย่าง:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">func swap(a, b *int) {\r\n    temp := *a\r\n    *a = *b\r\n    *b = temp\r\n}\r\n\r\nfunc main() {\r\n    x, y := 1, 2\r\n    swap(&amp;x, &amp;y)\r\n    fmt.Println(\"x:\", x, \"y:\", y) \u002F\u002F Output: x: 2 y: 1\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>","11_11zon_3681na6v7i.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fhyf97s911wgxmqy\u002F11_11zon_3681na6v7i.webp","2026-03-04 08:34:21.037Z","",{"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:34:09.187Z","3zp82zk9hqz13ko","การเขียนโปรแกรม Go","2026-04-10 16:08:08.225Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:34:17.826Z","v52y92wzlfib78r","nil Pointer","2026-04-10 16:08:11.531Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:34:20.096Z","hi69ad0xezh2yct","การควบคุมหน่วยความจำ","2026-04-10 16:08:12.272Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:26:32.195Z","3lpkahsesypeohl","Pointers","2026-04-10 16:07:30.526Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"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":71,"views":72},"wqxt7ag2gn7xcmk","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":66,"priority":67,"publish_at":68,"scheduled_at":13,"status":69,"updated":70,"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":66,"th":66},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","hkixn5qaznhwdjv",753,"hyf97s911wgxmqy",[20,25,30,35,40],"2025-01-22 05:01:35.818Z","เรียนรู้การประกาศและใช้งาน Pointers ในภาษา Go พร้อมวิธีการส่งตัวแปรเข้าไปในฟังก์ชันด้วยการอ้างอิงและการตรวจสอบ nil Pointer","go-pointers-memory-management","2026-04-25 02:47:31.310Z",{"th":77}]