Created
July 10, 2019 02:33
-
-
Save kai5263499/b02ec44b8b3b05cd6d278efe61d28434 to your computer and use it in GitHub Desktop.
Irvine Golang Meetup Code Challenge 7/9/2019
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"html/template" | |
"net/http" | |
"reflect" | |
) | |
type UserData struct { | |
FullName string `name:"fullname" desc:"Full name"` | |
Email string `name:"email" desc:"Email address"` | |
Phone string `name:"phone" desc:"Phone number"` | |
} | |
type FormField struct { | |
Name string | |
Desc string | |
} | |
type Page struct { | |
PageTitle string | |
FormFields []FormField | |
} | |
func main() { | |
page := Page{ | |
PageTitle: "hello world", | |
FormFields: make([]FormField, 0), | |
} | |
ud := UserData{} | |
st := reflect.TypeOf(ud) | |
for i := 0; i < st.NumField(); i++ { | |
field := st.Field(i) | |
formField := FormField{ | |
Name: field.Tag.Get("name"), | |
Desc: field.Tag.Get("desc"), | |
} | |
page.FormFields = append(page.FormFields, formField) | |
} | |
pageTemplate := `<html> | |
<h1>{{.PageTitle}}</h1> | |
<body> | |
<style></style> | |
<ul> | |
<form> | |
{{range .FormFields}} | |
<label>{{.Desc}}</label> | |
<input type="text" name="{{.Name}}" value=""/><br /> | |
{{end}} | |
</form> | |
</ul> | |
</body> | |
</html>` | |
t, _ := template.New("webpage").Parse(pageTemplate) | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
_ = t.Execute(w, page) | |
//fmt.Fprint(w, "Welcome to my website!") | |
}) | |
http.ListenAndServe(":8089", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment