Skip to main content

Text Data Return

In GoFrame, use the following methods to return text data to the client:

func (r *Response) Write(content ...interface{})
func (r *Response) WriteExit(content ...interface{})
func (r *Response) Writef(format string, params ...interface{})
func (r *Response) WritefExit(format string, params ...interface{})
func (r *Response) Writeln(content ...interface{})
func (r *Response) WritelnExit(content ...interface{})
func (r *Response) Writefln(format string, params ...interface{})
func (r *Response) WriteflnExit(format string, params ...interface{})

Methods suffixed with Exit exit the request after executing the response, skipping further execution. Methods suffixed with ln append a newline at the end of the response content.

These methods are used to respond to clients with text content formatted as text/html or text/plain. Parameters can be of any data type; non-string types typically convert content to JSON strings before returning to the client.

If the provided content is text, it can be plain text or HTML.

Responding with Plain Text

req.Response.Write("锦瑟无端五十弦")

Responding with Simple HTML

req.Response.Write("<h1>春蚕到死丝方尽</h1>")

Responding with Complex HTML

html := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Clock</title>
<style>
/* CSS styles omitted for brevity */
</style>
</head>
<body>
<div class="clock">
<!-- HTML structure omitted for brevity -->
</div>
<script>
// JavaScript script omitted for brevity
</script>
</body>
</html>
`
req.Response.Write(html)

Formatted Data Filling

html := `
<div>Name: %s</div>
<div>Age: %d</div>
`
req.Response.Writef(html, "林黛玉", 16)