<\/span><\/h1>\n\n\n\notp.go<\/strong><\/p>\n\n\n\npackage main\n\ntype iOtp interface {\n genRandomOTP(int) string\n saveOTPCache(string)\n getMessage(string) string\n sendNotification(string) error\n publishMetric()\n}\n\ntype otp struct {\n iOtp iOtp\n}\n\nfunc (o *otp) genAndSendOTP(otpLength int) error {\n otp := o.iOtp.genRandomOTP(otpLength)\n o.iOtp.saveOTPCache(otp)\n message := o.iOtp.getMessage(otp)\n err := o.iOtp.sendNotification(message)\n if err != nil {\n return err\n }\n o.iOtp.publishMetric()\n return nil\n}<\/code><\/pre>\n\n\n\nsms.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype sms struct {\n otp\n}\n\nfunc (s *sms) genRandomOTP(len int) string {\n randomOTP := \"1234\"\n fmt.Printf(\"SMS: generating random otp %s\\n\", randomOTP)\n return randomOTP\n}\n\nfunc (s *sms) saveOTPCache(otp string) {\n fmt.Printf(\"SMS: saving otp: %s to cache\\n\", otp)\n}\n\nfunc (s *sms) getMessage(otp string) string {\n return \"SMS OTP for login is \" + otp\n}\n\nfunc (s *sms) sendNotification(message string) error {\n fmt.Printf(\"SMS: sending sms: %s\\n\", message)\n return nil\n}\n\nfunc (s *sms) publishMetric() {\n fmt.Printf(\"SMS: publishing metrics\\n\")\n}<\/code><\/pre>\n\n\n\nemail.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype email struct {\n otp\n}\n\nfunc (s *email) genRandomOTP(len int) string {\n randomOTP := \"1234\"\n fmt.Printf(\"EMAIL: generating random otp %s\\n\", randomOTP)\n return randomOTP\n}\n\nfunc (s *email) saveOTPCache(otp string) {\n fmt.Printf(\"EMAIL: saving otp: %s to cache\\n\", otp)\n}\n\nfunc (s *email) getMessage(otp string) string {\n return \"EMAIL OTP for login is \" + otp\n}\n\nfunc (s *email) sendNotification(message string) error {\n fmt.Printf(\"EMAIL: sending email: %s\\n\", message)\n return nil\n}\n\nfunc (s *email) publishMetric() {\n fmt.Printf(\"EMAIL: publishing metrics\\n\")\n}<\/code><\/pre>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n smsOTP := &sms{}\n o := otp{\n iOtp: smsOTP,\n }\n o.genAndSendOTP(4)\n fmt.Println(\"\")\n emailOTP := &email{}\n o = otp{\n iOtp: emailOTP,\n }\n o.genAndSendOTP(4)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nSMS: generating random otp 1234\nSMS: saving otp: 1234 to cache\nSMS: sending sms: SMS OTP for login is 1234\nSMS: publishing metrics\n\nEMAIL: generating random otp 1234\nEMAIL: saving otp: 1234 to cache\nEMAIL: sending email: EMAIL OTP for login is 1234\nEMAIL: publishing metrics<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
<\/span>Full Working Code:<\/strong><\/span><\/h1>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype iOtp interface {\n genRandomOTP(int) string\n saveOTPCache(string)\n getMessage(string) string\n sendNotification(string) error\n publishMetric()\n}\n\ntype otp struct {\n iOtp iOtp\n}\n\nfunc (o *otp) genAndSendOTP(otpLength int) error {\n otp := o.iOtp.genRandomOTP(otpLength)\n o.iOtp.saveOTPCache(otp)\n message := o.iOtp.getMessage(otp)\n err := o.iOtp.sendNotification(message)\n if err != nil {\n return err\n }\n o.iOtp.publishMetric()\n return nil\n}\n\ntype sms struct {\n otp\n}\n\nfunc (s *sms) genRandomOTP(len int) string {\n randomOTP := \"1234\"\n fmt.Printf(\"SMS: generating random otp %s\\n\", randomOTP)\n return randomOTP\n}\n\nfunc (s *sms) saveOTPCache(otp string) {\n fmt.Printf(\"SMS: saving otp: %s to cache\\n\", otp)\n}\n\nfunc (s *sms) getMessage(otp string) string {\n return \"SMS OTP for login is \" + otp\n}\n\nfunc (s *sms) sendNotification(message string) error {\n fmt.Printf(\"SMS: sending sms: %s\\n\", message)\n return nil\n}\n\nfunc (s *sms) publishMetric() {\n fmt.Printf(\"SMS: publishing metrics\\n\")\n}\n\ntype email struct {\n otp\n}\n\nfunc (s *email) genRandomOTP(len int) string {\n randomOTP := \"1234\"\n fmt.Printf(\"EMAIL: generating random otp %s\\n\", randomOTP)\n return randomOTP\n}\n\nfunc (s *email) saveOTPCache(otp string) {\n fmt.Printf(\"EMAIL: saving otp: %s to cache\\n\", otp)\n}\n\nfunc (s *email) getMessage(otp string) string {\n return \"EMAIL OTP for login is \" + otp\n}\n\nfunc (s *email) sendNotification(message string) error {\n fmt.Printf(\"EMAIL: sending email: %s\\n\", message)\n return nil\n}\n\nfunc (s *email) publishMetric() {\n fmt.Printf(\"EMAIL: publishing metrics\\n\")\n}\n\nfunc main() {\n smsOTP := &sms{}\n o := otp{\n iOtp: smsOTP,\n }\n o.genAndSendOTP(4)\n fmt.Println(\"\")\n emailOTP := &email{}\n o = otp{\n iOtp: emailOTP,\n }\n o.genAndSendOTP(4)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nSMS: generating random otp 1234\nSMS: saving otp: 1234 to cache\nSMS: sending sms: SMS OTP for login is 1234\nSMS: publishing metrics\n\nEMAIL: generating random otp 1234\nEMAIL: saving otp: 1234 to cache\nEMAIL: sending email: EMAIL OTP for login is 1234\nEMAIL: publishing metrics<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Note: Interested in understanding how all other design patterns can be implemented in GO. Please see this full reference – All Design Patterns in Go (Golang) Table of Contents Introduction:ExampleFull Working Code:…<\/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":[26,3,4,93,27,92],"class_list":["post-724","post","type-post","status-publish","format-standard","hentry","category-tech","tag-design","tag-go","tag-golang","tag-method","tag-pattern","tag-template"],"yoast_head":"\n
Template Method Design Pattern 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