Recommended methods in Golang

In Golang structure , promoted methods work the same way as promoted fields. We use this concept in nested structure, where a structure is a field in another structure, just add the name of the structure to another structure and it works like anonymous field for the nested structure. The methods of that structure (other than the nested structure) are part of the nested structure, such type of methods are called promoted methods. In other words, promoted methods are methods implemented by the child structure and accessible by the parent structure.

 

Recommended methods in Golang Picture 1

Important points to remember:

  1. If the child and parent structures contain a method with the same name but different receiver type, then both methods are available in the parent structure as shown in Example 2. Here, both the child and parent structures contain methods with the same name.
  2. If a child structure contains two methods with the same name and the same receiver, then these methods are not advertised in the parent structure and if attempted, the compiler will report an error.

 

Example 1:

// Chương trình Go minh họa // khái niệm các phương thức được khuyến khích package main import "fmt" // Cấu trúc type details struct { // Trường // cấu trúc chi tiết name string age int gender string psalary int } // Cấu trúc lồng nhau type employee struct { post string eid int details } // Phương thức func (d details) promotmethod(tsalary int) int { return d.psalary * tsalary } func main() { // Khởi tạo các trường của // cơ cấu nhân viên values := employee{ post: "HR", eid: 4567, details: details{ name: "Sumit", age: 28, gender: "Male", psalary: 890, }, } // Các trường được khuyến khích của // cơ cấu nhân viên fmt.Println("Name: ", values.name) fmt.Println("Age: ", values.age) fmt.Println("Gender: ", values.gender) fmt.Println("Per day salary: ", values.psalary) // Phương thức được khuyến nghị của // cơ cấu nhân viên fmt.Println("Total Salary: ", values.promotmethod(30)) // Các trường bình thường của // cơ cấu nhân viên fmt.Println("Post: ", values.post) fmt.Println("Employee id: ", values.eid) }

Result:

Name: Sumit Age: 28 Gender: Male Per day salary: 890 Total Salary: 26700 Post: HR Employee id: 4567

Example 2:

// Chương trình Go minh họa // khái niệm của phương thức được khuyến nghị package main import "fmt" // Cấu trúc type details struct { // Các trường của // cấu trúc chi tiết name string age int gender string psalary int } // Phương thức 1 func (e employee) promotmethod(tarticle int) int { return e.particle * tarticle } // Cấu trúc lồng nhau type employee struct { post string particle int eid int details } // Phương thức 2 func (d details) promotmethod(tsalary int) int { return d.psalary * tsalary } // Phương thức chính func main() { // Khởi tạo các trường của // cơ cấu nhân viên values := employee{ post: "HR", eid: 4567, particle: 5, details: details{ name: "Sumit", age: 28, gender: "Male", psalary: 890, }, } // Các trường được khuyến nghị // cơ cấu nhân viên fmt.Println("Name: ", values.name) fmt.Println("Age: ", values.age) fmt.Println("Gender: ", values.gender) fmt.Println("Per day salary: ", values.psalary) // Phương thức được khuyến nghị // cơ cấu nhân viên fmt.Println("Total Salary: ", values.details.promotmethod(30)) // Các trường bình thường của // cơ cấu nhân viên fmt.Println("Post: ", values.post) fmt.Println("Employee id: ", values.eid) fmt.Println("Total Articles: ", values.promotmethod(30)) }

Result:

Name: Sumit Age: 28 Gender: Male Per day salary: 890 Total Salary: 150 Post: HR Employee id: 4567 Total Articles: 150
4 ★ | 1 Vote