closure คือฟังก์ชันที่อ้างอิงตัวแปรจาก scope รอบ ๆ ตัวมัน โดย "ปิดล้อม (closing over)" ตัวแปรเหล่านั้น ฟังก์ชันยังคงเข้าถึงตัวแปรเหล่านั้นได้แม้ฟังก์ชันที่ห่อหุ้มจะ return ไปแล้ว ฟังก์ชันใน Go เป็นค่าระดับชั้นหนึ่ง ทำให้ closure เป็นเครื่องมือที่พบบ่อยและทรงพลัง
closure พื้นฐาน
{
count :=
{
count++
count
}
}
counter := makeCounter()
fmt.Println(counter())
fmt.Println(counter())
other := makeCounter()
fmt.Println(other())
