Home > Article > Backend Development > Object-oriented features and application examples in Go language
Object-oriented features and application examples in Go language
Abstract: This article will introduce the features and application examples of object-oriented programming in Go language, and explain in detail how to use object-oriented programming in Go language through code examples programming with thoughts.
Introduction: Object-oriented programming is a very widely used programming paradigm. It encapsulates data and operations in an object and implements program logic through interactions between objects. In the Go language, object-oriented programming also has unique characteristics and application examples, which will be introduced in detail in this article.
1. Object-oriented features
Sample Code 1:
package main import "fmt" type Rect struct { width float64 height float64 } func (r *Rect) Area() float64 { return r.width * r.height } func main() { rect := Rect{width: 3, height: 4} fmt.Println(rect.Area()) }
Sample code 2:
package main import "fmt" type Animal struct { name string } func (a *Animal) SayName() { fmt.Println("My name is", a.name) } type Dog struct { Animal } func main() { dog := Dog{Animal: Animal{name: "Tom"}} dog.SayName() }
Sample code 3:
package main import "fmt" type Shape interface { Area() float64 } type Rect struct { width float64 height float64 } func (r *Rect) Area() float64 { return r.width * r.height } type Circle struct { radius float64 } func (c *Circle) Area() float64 { return 3.14 * c.radius * c.radius } func printArea(s Shape) { fmt.Println("Area:", s.Area()) } func main() { rect := &Rect{width: 3, height: 4} circle := &Circle{radius: 2} printArea(rect) printArea(circle) }
2. Object-oriented application examples
Sample code 4:
package main import "fmt" type Shape interface { Area() float64 Perimeter() float64 } type Rectangle struct { length float64 width float64 } func (r *Rectangle) Area() float64 { return r.length * r.width } func (r *Rectangle) Perimeter() float64 { return 2 * (r.length + r.width) } type Circle struct { radius float64 } func (c *Circle) Area() float64 { return 3.14 * c.radius * c.radius } func (c *Circle) Perimeter() float64 { return 2 * 3.14 * c.radius } func main() { rectangle := &Rectangle{length: 3, width: 4} circle := &Circle{radius: 2} shapes := []Shape{rectangle, circle} for _, shape := range shapes { fmt.Println("Area:", shape.Area()) fmt.Println("Perimeter:", shape.Perimeter()) } }
Sample code 5:
package main import "fmt" type Product struct { name string price float64 } type ShoppingCart struct { products []*Product } func (sc *ShoppingCart) AddProduct(product *Product) { sc.products = append(sc.products, product) } func (sc *ShoppingCart) RemoveProduct(name string) { for i, product := range sc.products { if product.name == name { sc.products = append(sc.products[:i], sc.products[i+1:]...) break } } } func (sc *ShoppingCart) CalculateTotalPrice() float64 { totalPrice := 0.0 for _, product := range sc.products { totalPrice += product.price } return totalPrice } func main() { product1 := &Product{name: "Apple", price: 2.5} product2 := &Product{name: "Banana", price: 1.5} product3 := &Product{name: "Orange", price: 1.0} shoppingCart := &ShoppingCart{} shoppingCart.AddProduct(product1) shoppingCart.AddProduct(product2) shoppingCart.AddProduct(product3) fmt.Println("Total Price:", shoppingCart.CalculateTotalPrice()) shoppingCart.RemoveProduct("Banana") fmt.Println("Total Price:", shoppingCart.CalculateTotalPrice()) }
Summary: This article introduces the characteristics and application examples of object-oriented programming in Go language, and details how to use it in Go language through code examples Program with object-oriented thinking. Object-oriented programming can improve the reusability and scalability of code, and can better organize and manage program logic. It is a very important and practical programming paradigm.
The above is the detailed content of Object-oriented features and application examples in Go language. For more information, please follow other related articles on the PHP Chinese website!