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.