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.
Code
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
cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
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=`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.