Golang : Auto-generate reply email with text/template package
Problem :
You have customers that ask question via email that sometimes bordering repetitive questioning or simply refused to read the Frequently Asked Question section. What should you do?
Solution :
Instead of cracking your head to write reply email each time, why not create an email auto-generator? It can be done easily with the text/template
package. For this example, I will keep it simple. No artificial intelligence stuff yet( I'm working on it ) and auto email out the reply yet(shouldn't be hard to implement)
Here you go!
package main
import (
"fmt"
"log"
"os"
"text/template"
)
func main() {
// Define a email template.
const email = `
Hey {{.Name}},
{{if .Attended}}
Have you read FAQ section yet?{{else}}
It is a shame that you choose not to understand my first email reply!{{end}}
{{with .Gift}}Thank you for your equiry about {{.}} by the way.
{{end}}
Best wishes,
Gossie
`
// Prepare some data to insert into the template.
type Recipient struct {
Name, Gift string
Attended bool
}
var recipients = []Recipient{
{"Aunt Fonda", "dragon bone pottery set", true},
{"Scott", "ancient map to the castle in the sky", false},
{"Jessie", "", false},
}
// Create a new template and parse the letter into it.
t := template.Must(template.New("email").Parse(email))
// Execute the template for each recipient.
for _, r := range recipients {
err := t.Execute(os.Stdout, r)
fmt.Println("----------------")
if err != nil {
log.Println("executing template:", err)
}
}
}
Output :
Hey Aunt Fonda,
Have you read FAQ section yet?
Thank you for your equiry about dragon bone pottery set by the way.
Best wishes,
Gossie
Hey Scott,
It is a shame that you choose not to understand my first email reply!
Thank you for your equiry about ancient map to the castle in the sky by the way.
Best wishes,
Gossie
Hey Jessie,
It is a shame that you choose not to understand my first email reply!
Best wishes,
Gossie
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+51.8k Golang : How to get time in milliseconds?
+17.4k Golang : Linked list example
+4.6k MariaDB/MySQL : How to get version information
+79.6k Golang : How to return HTTP status code?
+24.5k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+15.2k nginx: [emerg] unknown directive "ssl"
+29k Golang : Get first few and last few characters from string
+22.7k Golang : untar or extract tar ball archive example
+5.3k Python : Delay with time.sleep() function example
+8.9k Golang : Build and compile multiple source files
+13.4k Golang : Read XML elements data with xml.CharData example
+19.8k Golang : How to get time from unix nano example