# Generics (Go) > Type parameters, constraints, comparable, any, ฟังก์ชัน generic, ชนิดข้อมูล generic, การอนุมานชนิดข้อมูล - 20 คำถามสัมภาษณ์ - Senior - [คำถามสัมภาษณ์: Go](https://sharpskill.dev/th/technologies/go/interview-questions.md) ## 1. type parameter ใน Go คืออะไร? **คำตอบ** type parameter คือตัวแปรชนิดข้อมูลที่ใช้ในการนิยามฟังก์ชันหรือชนิดข้อมูลแบบ generic โดยประกาศไว้ภายในวงเล็บเหลี่ยม มันช่วยให้เขียนโค้ดที่นำกลับมาใช้ใหม่ได้ซึ่งทำงานกับชนิดข้อมูลที่แตกต่างกันได้โดยไม่เสีย type safety type parameter จะถูกสร้างอินสแตนซ์ด้วย type argument ที่เป็นรูปธรรมเมื่อใช้งาน ทำให้คอมไพเลอร์สามารถตรวจสอบความสอดคล้องของชนิดข้อมูลในเวลาคอมไพล์แทนที่จะเป็นในเวลารันไทม์ ## 2. ไวยากรณ์ที่ถูกต้องในการประกาศฟังก์ชัน generic แบบง่ายคืออะไร? **คำตอบ** ไวยากรณ์ที่ถูกต้องในการประกาศฟังก์ชัน generic ใช้วงเล็บเหลี่ยมทันทีหลังชื่อฟังก์ชันเพื่อนิยาม type parameter constraint any อนุญาตให้รับชนิดข้อมูลใดก็ได้ คล้ายกับ interface{} แต่มีความหมายที่ชัดเจนกว่าในบริบทของ generics ไวยากรณ์นี้ถูกนำมาใช้ใน Go 1.18 เพื่อให้สามารถเขียนโปรแกรมแบบ generic ได้ในขณะที่ยังคงความเรียบง่ายของภาษาไว้ ## 3. constraint 'any' ใน Go แทนอะไร? **คำตอบ** constraint any เป็น alias สำหรับ interface{} ซึ่งแทนเซตของชนิดข้อมูลที่เป็นไปได้ทั้งหมดใน Go มันช่วยให้ type parameter รับชนิดข้อมูลใดก็ได้โดยไม่มีข้อจำกัด ให้ความยืดหยุ่นสูงสุด แม้จะสะดวก แต่การใช้ any หมายถึงการสละการตรวจสอบชนิดข้อมูลเฉพาะ และมักต้องใช้ type assertion เพื่อเข้าถึงเมธอดหรือคุณสมบัติของชนิดข้อมูลพื้นฐาน ## มีอีก 17 คำถาม - ความแตกต่างหลักระหว่าง 'any' กับ 'comparable' ในฐานะ constraint คืออะไร? - จะประกาศชนิดข้อมูล generic แบบกำหนดเองเช่น stack ได้อย่างไร? สมัครฟรี: https://sharpskill.dev/th/login ## หัวข้อสัมภาษณ์ Go อื่นๆ - [พื้นฐาน Go](https://sharpskill.dev/th/technologies/go/interview-questions/go-basics.md): 25 คำถาม, Junior - [โครงสร้างข้อมูล Go](https://sharpskill.dev/th/technologies/go/interview-questions/go-data-structures.md): 20 คำถาม, Junior - [อินเทอร์เฟซใน Go](https://sharpskill.dev/th/technologies/go/interview-questions/go-interfaces.md): 18 คำถาม, Junior - [การจัดการข้อผิดพลาด](https://sharpskill.dev/th/technologies/go/interview-questions/error-handling.md): 20 คำถาม, Junior - [พื้นฐาน Goroutine](https://sharpskill.dev/th/technologies/go/interview-questions/goroutines-basics.md): 20 คำถาม, Junior - [Channels](https://sharpskill.dev/th/technologies/go/interview-questions/channels.md): 22 คำถาม, Junior - [Go Modules](https://sharpskill.dev/th/technologies/go/interview-questions/go-modules.md): 18 คำถาม, Mid-Level - [เซิร์ฟเวอร์ HTTP](https://sharpskill.dev/th/technologies/go/interview-questions/http-server.md): 24 คำถาม, Mid-Level - [ไคลเอนต์ HTTP](https://sharpskill.dev/th/technologies/go/interview-questions/http-client.md): 18 คำถาม, Mid-Level - [การเข้ารหัส JSON](https://sharpskill.dev/th/technologies/go/interview-questions/json-encoding.md): 20 คำถาม, Mid-Level - [Database/SQL](https://sharpskill.dev/th/technologies/go/interview-questions/database-sql.md): 22 คำถาม, Mid-Level - [แพ็กเกจ context](https://sharpskill.dev/th/technologies/go/interview-questions/context-package.md): 20 คำถาม, Mid-Level - [Testing](https://sharpskill.dev/th/technologies/go/interview-questions/testing.md): 22 คำถาม, Mid-Level - [รูปแบบ Concurrency](https://sharpskill.dev/th/technologies/go/interview-questions/concurrency-patterns.md): 24 คำถาม, Mid-Level - [ไพรมิทีฟการซิงโครไนซ์](https://sharpskill.dev/th/technologies/go/interview-questions/sync-primitives.md): 22 คำถาม, Mid-Level - [เฟรมเวิร์กเว็บ Go](https://sharpskill.dev/th/technologies/go/interview-questions/go-web-frameworks.md): 20 คำถาม, Mid-Level - [การออกแบบ REST API](https://sharpskill.dev/th/technologies/go/interview-questions/rest-api-design.md): 20 คำถาม, Mid-Level - [gRPC](https://sharpskill.dev/th/technologies/go/interview-questions/grpc.md): 22 คำถาม, Mid-Level - [Reflection](https://sharpskill.dev/th/technologies/go/interview-questions/reflection.md): 20 คำถาม, Senior - [การจัดการหน่วยความจำ](https://sharpskill.dev/th/technologies/go/interview-questions/memory-management.md): 24 คำถาม, Senior - [การเพิ่มประสิทธิภาพการทำงาน](https://sharpskill.dev/th/technologies/go/interview-questions/performance-optimization.md): 22 คำถาม, Senior - [Go Design Patterns](https://sharpskill.dev/th/technologies/go/interview-questions/design-patterns.md): 24 คำถาม, Senior - [Microservices](https://sharpskill.dev/th/technologies/go/interview-questions/microservices.md): 24 คำถาม, Senior - [ความปลอดภัยและการยืนยันตัวตน](https://sharpskill.dev/th/technologies/go/interview-questions/security-authentication.md): 24 คำถาม, Senior - [Docker & Containerization](https://sharpskill.dev/th/technologies/go/interview-questions/docker-containerization.md): 20 คำถาม, Senior - [Kubernetes Basics](https://sharpskill.dev/th/technologies/go/interview-questions/kubernetes-basics.md): 22 คำถาม, Senior - [Go ขั้นสูง](https://sharpskill.dev/th/technologies/go/interview-questions/advanced-go.md): 20 คำถาม, Senior - [การพัฒนา CLI](https://sharpskill.dev/th/technologies/go/interview-questions/cli-development.md): 20 คำถาม, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/th/technologies/go/interview-questions/generics