与Golang等效的mysql_real_escape_string

Kev*_*rke 1 security escaping go

我想逃避构成数据库查询一部分的值,但我不能使用参数化查询.

Go是否有相当于mysql_real_escape_string我可以用来逃避查询值的PHP ?

Sha*_*bob 5

我想出了自己的解决方案来自己创建这个功能.
希望它对某人有用.

func MysqlRealEscapeString(value string) string {
    replace := map[string]string{"\\":"\\\\", "'":`\'`, "\\0":"\\\\0", "\n":"\\n", "\r":"\\r", `"`:`\"`, "\x1a":"\\Z"}

    for b, a := range replace {
        value = strings.Replace(value, b, a, -1)
    }

    return value
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,在 Go 中,映射是无序的,因此替换可能以任何顺序发生,但必须在任何其他规则之前应用双反斜杠。您可能想使用 [][2]string 作为规则。 (2认同)