golang 在编译时删除 const 字符串中的字符(用于可读性)(空格、 和 t)



>空格对于缩进url,sql查询很有用,以使其更具可读性。有没有办法在 golang 编译时从 const 字符串中删除字符?

ex: (runtime version)
const url = `https://example.com/path?
attr1=test
&attr2=test
`
// this is the code to be replaced
urlTrim := strings.Replace(
strings.Replace(url, "n", "", -1)
)

常量表达式不能包含函数调用(少数内置函数除外(。因此,您想要的不能使用原始字符串文字来完成。

如果您使用多行的目的只是为了可读性,只需使用多个文字并将它们连接起来:

const url = "https://example.com/path?" +
    "attr1=test" +
    "&attr2=test"

在Go Playground上尝试一下。

请参阅相关问题:初始化常量变量

最新更新