对比于PHP“<<<END END;”功能,有时候想输出一大段文字,并且字符串里面带有双引号,Go中对双引号的使用有明显规定。
// PHP代码
$str = <<<END
//some codes you want to write
//the end of the block should be a new line and have no space
END;
如下GO使用,使用“ `[字符串内容]` ”即可:
// Go代码
package main
import (
"fmt"
)
func main() {
var str string = "2021"
var div string = `
<div style="color:blue;" data-id='1'>
<script>alert("js")</script>
<!-- 动态渲染html内容 -->
<h3 class="red">
` + str + `
</h3>
</div>
`
fmt.Println(div)
}
Gin输出到页面html:
ctx.Header("Content-Type", "text/html; charset=utf-8")
ctx.String(200, `<p>html代码</p>`)
-