Home >Backend Development >Golang >What are the best practices when creating custom types in Golang?
Following the following best practices when creating custom types in Go can improve code quality: capitalize the first letter, use recommended type aliases, use struct, use embed to avoid duplication, and implement interfaces. These practices enhance organization, readability, and maintainability, such as the shopping cart type shown in the practical example.
Best practices when creating custom types using Go
Creating custom types in Go can enhance the organization of your code performance, readability and maintainability. Following some best practices can ensure that your custom types are well designed and effective.
Capitalize the first letter
According to Go convention, the first letter of the custom type name should be capitalized. This helps distinguish custom types from other identifiers.
type Customer struct { // ... } type Order struct { // ... }
Use recommended type aliases
For commonly used built-in types, it is recommended to use recommended type aliases. For example, for errors, use the error
type alias instead of interface{}
.
type MyError struct { msg string } func (e MyError) Error() string { return e.msg } func CheckSomething() error { // ... }
Use struct
Use struct
to represent custom data types with multiple fields. Combining related fields into a struct
improves code readability and maintainability.
type Person struct { Name string Age int Address string }
Use embed to avoid duplication
When multiple custom types have the same subset fields, you can use embed
to share the fields embedded in one of the types. This avoids duplication of code and keeps code clean.
type Person struct { Name string Age int Contact Information } type Information struct { Address string Phone string }
Implementing the interface
Implementing the interface allows the custom type to have additional functionality. This can be achieved by defining type methods that satisfy the interface method signature.
type Shape interface { Area() float64 } type Rectangle struct { Width float64 Height float64 } func (r Rectangle) Area() float64 { return r.Width * r.Height }
Practical case
Let us create a custom type that represents the shopping basket:
type Cart struct { Items []Item TotalPrice float64 } type Item struct { Name string Price float64 Qty int } func main() { // 创建购物车 cart := &Cart{} // 向购物车添加物品 item1 := Item{Name: "Apple", Price: 1.0, Qty: 1} item2 := Item{Name: "Banana", Price: 1.5, Qty: 2} cart.Items = append(cart.Items, item1, item2) // 计算总价 for _, item := range cart.Items { cart.TotalPrice += item.Price * float64(item.Qty) } // 打印购物车内容 fmt.Printf("Shopping cart contents:\n\n") for _, item := range cart.Items { fmt.Printf("%s (%dx): $%.2f\n", item.Name, item.Qty, item.Price) } // 打印总价 fmt.Printf("\nTotal price: $%.2f\n", cart.TotalPrice) }
This custom type encapsulates the data of the shopping cart , simplifying the process of managing multiple items and calculating total prices.
The above is the detailed content of What are the best practices when creating custom types in Golang?. For more information, please follow other related articles on the PHP Chinese website!