Gensics

Back to Home

Structs และ Interfaces ใน Go

NPXVERSE Team14 กรกฎาคม 2567Object-Oriented
GolangStructsInterfacesOOP

Structs ใน Go

Structs เป็นวิธีการจัดกลุ่มข้อมูลที่เกี่ยวข้องกันใน Go

การประกาศ Struct

package main import "fmt" type Person struct { Name string Age int Email string Address Address } type Address struct { Street string City string Country string } func main() { // การสร้าง struct instance person1 := Person{ Name: "John Doe", Age: 30, Email: "john@example.com", Address: Address{ Street: "123 Main St", City: "Bangkok", Country: "Thailand", }, } fmt.Printf("Person: %+v\n", person1) fmt.Println("Name:", person1.Name) fmt.Println("City:", person1.Address.City) }

Methods ใน Structs

package main import "fmt" type Rectangle struct { Width float64 Height float64 } // Method with value receiver func (r Rectangle) Area() float64 { return r.Width * r.Height } // Method with pointer receiver func (r *Rectangle) Scale(factor float64) { r.Width *= factor r.Height *= factor } // Method ที่ return string func (r Rectangle) String() string { return fmt.Sprintf("Rectangle{Width: %.2f, Height: %.2f}", r.Width, r.Height) } func main() { rect := Rectangle{Width: 10, Height: 5} fmt.Println("Rectangle:", rect) fmt.Println("Area:", rect.Area()) rect.Scale(2) fmt.Println("After scaling:", rect) fmt.Println("New area:", rect.Area()) }

Interfaces ใน Go

Interfaces ใน Go ช่วยให้เราสามารถกำหนด behavior ที่ types ควรจะมี

การประกาศ Interface

package main import "fmt" // Interface definition type Shape interface { Area() float64 Perimeter() float64 } type Circle struct { Radius float64 } type Rectangle struct { Width, Height float64 } // Circle implements Shape interface func (c Circle) Area() float64 { return 3.14159 * c.Radius * c.Radius } func (c Circle) Perimeter() float64 { return 2 * 3.14159 * c.Radius } // Rectangle implements Shape interface func (r Rectangle) Area() float64 { return r.Width * r.Height } func (r Rectangle) Perimeter() float64 { return 2 * (r.Width + r.Height) } // Function that accepts any Shape func printShapeInfo(s Shape) { fmt.Printf("Area: %.2f\n", s.Area()) fmt.Printf("Perimeter: %.2f\n", s.Perimeter()) } func main() { circle := Circle{Radius: 5} rectangle := Rectangle{Width: 10, Height: 3} fmt.Println("Circle:") printShapeInfo(circle) fmt.Println("\nRectangle:") printShapeInfo(rectangle) }

Interface ขั้นสูง

package main import "fmt" // Empty interface func printAnything(v interface{}) { fmt.Printf("Value: %v, Type: %T\n", v, v) } // Type assertion func checkType(v interface{}) { switch v := v.(type) { case string: fmt.Printf("String: %s (length: %d)\n", v, len(v)) case int: fmt.Printf("Integer: %d\n", v) case bool: fmt.Printf("Boolean: %t\n", v) default: fmt.Printf("Unknown type: %T\n", v) } } func main() { printAnything("Hello") printAnything(42) printAnything(true) fmt.Println("\nType checking:") checkType("Go Language") checkType(2024) checkType(false) checkType(3.14) }

Composition Pattern

package main import "fmt" type Engine struct { Horsepower int Type string } func (e Engine) Start() { fmt.Printf("Starting %s engine with %d HP\n", e.Type, e.Horsepower) } type Car struct { Brand string Model string Engine Engine // Composition } func (c Car) Drive() { c.Engine.Start() fmt.Printf("Driving %s %s\n", c.Brand, c.Model) } func main() { car := Car{ Brand: "Toyota", Model: "Camry", Engine: Engine{ Horsepower: 200, Type: "V6", }, } car.Drive() // สามารถเข้าถึง Engine methods ได้โดยตรง car.Engine.Start() }

Structs และ Interfaces เป็นพื้นฐานสำคัญใน Go ที่ช่วยให้เราสามารถออกแบบโค้ดที่มีโครงสร้างชัดเจน และใช้งานซ้ำได้