{"id":2286,"date":"2020-06-20T02:21:40","date_gmt":"2020-06-19T20:51:40","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=2286"},"modified":"2020-06-20T02:21:53","modified_gmt":"2020-06-19T20:51:53","slug":"method-chaining-go","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/method-chaining-go\/","title":{"rendered":"Method Chaining in Go (Golang)"},"content":{"rendered":"\n
For method chaining to be possible, the methods in the chain should return the receiver. Returning the receiver for the last method in the chain is optional. <\/p>\n\n\n\n
Let’s see an example of method chaining.<\/p>\n\n\n\n
package main\n\nimport \"fmt\"\n\ntype employee struct {\n\tname string\n\tage int\n\tsalary int\n}\n\nfunc (e employee) printName() employee {\n\tfmt.Printf(\"Name: %s\\n\", e.name)\n\treturn e\n}\n\nfunc (e employee) printAge() employee {\n\tfmt.Printf(\"Age: %d\\n\", e.age)\n\treturn e\n}\n\nfunc (e employee) printSalary() {\n\tfmt.Printf(\"Salary: %d\\n\", e.salary)\n}\n\nfunc main() {\n\temp := employee{name: \"Sam\", age: 31, salary: 2000}\n\temp.printName().printAge().printSalary()\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nName: Sam\nAge: 31\nSalary: 2000<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"For method chaining to be possible, the methods in the chain should return the receiver. Returning the receiver for the last method in the chain is optional. Let’s see an example of…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4],"class_list":["post-2286","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\n
Method Chaining in Go (Golang) - Welcome To Golang By Example<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n