Skip to main content

文本数据返回

GoFrame中通过以下方法返回文本数据到客户端:

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{})

以上方法中,带有Exit的表示执行完响应之后就退出本次请求,不再执行后面的内容。带有ln的表示会在响应内容的末尾追加换行符。

以上方法用于向客户端响应文本内容。内容格式为text/htmltext/plain,参数可以是任意数据类型,非字符串类型通常会将内容进行json转为字符串后返回到客户端。

如果提供参数为文本,可以是普通文本也可以是HTML文本。

响应简单文本

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

响应简单HTML

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

响应复杂HTML

html := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Clock</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800;900&display=swap');
*
{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body
{
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #acbaca;
}
.clock
{
position: relative;
width: 300px;
height: 300px;
background: #c9d5e0;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50px;
box-shadow: 30px 30px 30px -10px rgba(0,0,0,0.15),
inset 15px 15px 10px rgba(255,255,255,0.75),
-15px -15px 35px rgba(255,255,255,0.55),
inset -1px -1px 10px rgba(0,0,0,0.2);
}
.clock::before
{
content: '';
position: absolute;
width: 4px;
height: 4px;
background: #e91e63;
border-radius: 50%;
z-index: 1000;
box-shadow: 0 0 0 1px #e91e63,
0 0 0 3px #fff,
0 0 5px 5px rgba(0,0,0,0.15);
}
.clock .numbers
{
position: absolute;
inset: 35px;
background: #152b4a;
border-radius: 50%;
box-shadow: 5px 5px 15px #152b4a66,
inset 5px 5px 5px rgba(255,255,255,0.55),
-6px -6px 10px rgba(255,255,255,1);
}
.clock .numbers span
{
position: absolute;
inset: 5px;
text-align: center;
color: #fff;
font-size: 1.25em;
transform: rotate(calc(90deg * var(--i)));
}
.clock .numbers span b
{
font-weight: 600;
display: inline-block;
transform: rotate(calc(-90deg * var(--i)));
}
.clock .numbers::before
{
content: '';
position: absolute;
inset: 35px;
background: linear-gradient(#2196f3,#e91e63);
border-radius: 50%;
animation: animate 2s linear infinite;
}
@keyframes animate
{
0%
{
transform: rotate(360deg);
}
100%
{
transform: rotate(0deg);
}
}
.clock .numbers::after
{
content: '';
position: absolute;
inset: 38px;
background: #152b4a;
border-radius: 50%;
}
.clock .numbers .circle
{
position: absolute;
inset: 0;
border-radius: 50%;
display: flex;
justify-content: center;
z-index: 10;
}
.clock .numbers .circle i
{
position: absolute;
width: 3px;
height: 50%;
background: #fff;
transform-origin: bottom;
}
.clock .numbers .circle#hr i
{
transform: scaleY(0.3);
width: 4px;
}
.clock .numbers .circle#mn i
{
transform: scaleY(0.45);
}
.clock .numbers .circle#sc i
{
width: 2px;
transform: scaleY(0.55);
background: #e91e63;
box-shadow: 0 30px 0 #e91e63;
}
</style>
</head>
<body>
<div class="clock">
<div class="numbers">
<span style="--i:0;"><b>12</b></span>
<span style="--i:1;"><b>3</b></span>
<span style="--i:2;"><b>6</b></span>
<span style="--i:3;"><b>9</b></span>
<div class="circle" id="hr"><i></i></div>
<div class="circle" id="mn"><i></i></div>
<div class="circle" id="sc"><i></i></div>
</div>
</div>
<script>
let hr = document.querySelector('#hr');
let mn = document.querySelector('#mn');
let sc = document.querySelector('#sc');

setInterval(()=>{
let day = new Date();
let hh = day.getHours() * 30;
let mm = day.getMinutes() * 6;
let ss = day.getSeconds() * 6;

hr.style.transform = 'rotateZ(' + hh+(mm/12) + 'deg)';
mn.style.transform = 'rotateZ(' + mm + 'deg)';
sc.style.transform = 'rotateZ(' + ss + 'deg)';
})
</script>
</body>
</html>
`
req.Response.Write(html)

格式化数据填充

html := `
<div>姓名:%s</div>
<div>年龄:%d</div>
`
req.Response.Writef(html, "林黛玉", 16)