Table of Contents
This updated guide explores recommended methods in golang with practical details, clear explanations, and useful takeaways. 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.

Important points to remember:
- 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.
- 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
FAQ
What is the main takeaway about Recommended Methods in Golang?
The key is to understand the core idea, compare the available options, and apply the practical guidance that matches your situation.
What should beginners know about Recommended Methods in Golang?
Start with the essential steps and terminology. Avoid changing advanced settings until you understand how they affect performance, privacy, cost, or compatibility.
How can I use this information in practice?
Follow the relevant section in order, test one change at a time, and confirm the result before moving to the next step or recommendation.
Reader Comments 0
Sign in with email or Google to join the discussion.