package templates import "fmt" // Table wrapper definitions const table_open string = `` const table_close string = `
` // Header definitions const table_head_open string = `` const table_head_close string = `` const table_head_row string = `%s` // Body definitions const table_body_open string = `` const table_body_close string = `` const table_body_row string = `%v` // Error message const query_error_message string = `

Query Error: %s

` const query_error_message_blank string = `` func ErrorQueryResults(e error) string { return table_open + table_close + fmt.Sprintf(query_error_message, e.Error()) } func QueryResults(cols []string, rows []map[string]interface{}) string { head := generateHead(cols) body := table_body_open for _, row := range rows { body += generateRow(cols, row) } body += table_body_close return table_open + head + body + table_close + query_error_message_blank } // Generate the tables head row func generateHead(cols []string) string { html := table_head_open for _, col := range cols { html += fmt.Sprintf(table_head_row, col) } return html + table_head_close } func generateRow(cols []string, data map[string]interface{}) string { row := "" for _, col := range cols { str, ok := data[col].(string) if !ok { str = "NULL" } row += fmt.Sprintf(table_body_row, str) } return row + "" }