Raw String Literals Hold Multiline Text

Keep JSON and multi-line SQL readable in Go source with backtick raw string literals, where backslashes and quotes stay literal and newlines are preserved.

GoData Integrity

Code

gosnippet
22 lines
package main

import (
	"fmt"
)

func main() {
	// 反引号原样字符串:反斜杠与双引号都不需要转义
	payload := `{"host":"db.internal","retries":3}`
	fmt.Println("json:", payload)

	query := `SELECT id, email
FROM accounts
WHERE active = true`
	fmt.Println("sql:")
	fmt.Println(query)

	// 对比解释型字符串:同样内容必须写成转义形式
	interpreted := "line1\nline2"
	fmt.Printf("interpreted len=%d raw len=%d\n", len(interpreted), len(`line1
line2`))
}

How it works

Raw String Literals Hold Multiline Text reduces to the function main.

Caveats

  • Self-contained — no imports beyond what the runtime already provides.

Using it

The signature to start from: func main()

The file announces its steps in the output: json:, sql:, interpreted len=.

Key statements

payload := `{"host":"db.internal","retries":3}`

Interactive Tools for This Topic

FAQ

What does Raw String Literals Hold Multiline Text print when run?
The script runs standalone; its 3 labeled outputs include `json:`, `sql:`, `interpreted len=`.