# การจัดการข้อผิดพลาด (Go) > Error type, error wrapping, errors.Is, errors.As, custom errors, panic/recover - 20 คำถามสัมภาษณ์ - Junior - [คำถามสัมภาษณ์: Go](https://sharpskill.dev/th/technologies/go/interview-questions.md) ## 1. อินเทอร์เฟซใดที่ใช้แทนข้อผิดพลาดใน Go? **คำตอบ** อินเทอร์เฟซ error เป็นอินเทอร์เฟซที่กำหนดไว้ล่วงหน้าใน Go สำหรับแทนสภาวะข้อผิดพลาด มันมีเมธอด Error() เพียงหนึ่งเดียวที่คืนค่าสตริงที่อธิบายข้อผิดพลาด ค่า nil แทนการไม่มีข้อผิดพลาด นี่คืออินเทอร์เฟซตามแบบแผนที่แพ็กเกจทั้งหมดของไลบรารีมาตรฐานและนักพัฒนา Go ใช้เพื่อส่งสัญญาณและส่งต่อข้อผิดพลาด ## 2. จะสร้างข้อผิดพลาดอย่างง่ายพร้อมข้อความแบบคงที่ใน Go ได้อย่างไร? **คำตอบ** ฟังก์ชัน errors.New() จากแพ็กเกจ errors สร้างข้อผิดพลาดอย่างง่ายพร้อมข้อความแบบคงที่ มันคืนค่าชนิด error ที่ implement อินเทอร์เฟซ error ด้วยข้อความที่ให้มา แนวทางนี้แนะนำสำหรับการสร้าง sentinel error (ข้อผิดพลาดที่กำหนดไว้ล่วงหน้าเช่น os.ErrNotExist) หรือข้อผิดพลาดอย่างง่ายที่ไม่มีบริบทแบบไดนามิก สำหรับข้อความแบบไดนามิก ให้ใช้ fmt.Errorf() ## 3. ฟังก์ชันใดที่อนุญาตให้สร้างข้อผิดพลาดพร้อมข้อความที่จัดรูปแบบแบบไดนามิก? **คำตอบ** ฟังก์ชัน fmt.Errorf() สร้างข้อผิดพลาดพร้อมข้อความที่จัดรูปแบบแบบไดนามิกโดยใช้ format verb (เช่น %s, %d, %v) นอกจากนี้ยังรองรับ verb พิเศษ %w เพื่อห่อหุ้มข้อผิดพลาดที่มีอยู่ ซึ่งเป็นการรักษาห่วงโซ่ข้อผิดพลาดไว้สำหรับการตรวจสอบในภายหลัง นี่คือวิธีที่แนะนำสำหรับการเพิ่มบริบทให้กับข้อผิดพลาดในขณะที่ยังคงข้อผิดพลาดเดิมไว้ ## มีอีก 17 คำถาม - format verb ใดที่อนุญาตให้ห่อหุ้มข้อผิดพลาดด้วย fmt.Errorf()? - ฟังก์ชันใดที่ตรวจสอบว่าข้อผิดพลาดที่ถูกห่อหุ้มตรงกับข้อผิดพลาดที่ระบุหรือไม่? สมัครฟรี: 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 - [พื้นฐาน 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 - [Generics](https://sharpskill.dev/th/technologies/go/interview-questions/generics.md): 20 คำถาม, 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/error-handling