Golang如何逃离蜱虫 [英] Golang how to escape back ticks

查看:137
本文介绍了Golang如何逃离蜱虫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySQL要求使用保留字的表格作为后备符号。我有一个表格角色,它是一个保留字,但我已经把我的查询放回去了,所以我可以把它写成多行(这是一个玩具查询,大的查询不会放在一行上......)。



我如何逃避背tick?

这是我的代码:

  dbmap:= db.InitDb()

var roles [] entities.Role
query:=
`<<使用SO的代码编辑器小部件很难看到,但是这里有一个后退勾号
SELECT *
FROM`Role`<<<<<需要转移
`<<很难看到,但是这里是一个返回值

_,err:= dbmap.Select(& roles,query,nil)
if err!= nil {
panic (err)
}

fmt.Println(roles)



$ b

  dbmap:=>解决方案

你不能在反引号内部反斜杠,但你可以这样做: db.InitDb()

var roles [] entities.Role
query:=`
SELECT *
FROM`+`Role`

_,err:= dbmap.Select(& roles,query,nil)
if err!= nil {
panic(err)
}

fmt.Println(角色)


MySQL requires tables that shadow reserved words to be back ticked. I have a table Role which is a reserved word, but I have already put my query in back ticks so I can write it over multiple lines (this is a toy query, large ones will not fit on one line...).

How do I escape the back ticks?

Here is my code:

dbmap := db.InitDb()

var roles []entities.Role
query :=
    ` << Difficult to see with SO's code editor widget, but here is a back tick
SELECT *
FROM `Role` <<< Needs escaping
`  << Difficult to see, but here is a back tick

_, err := dbmap.Select(&roles, query, nil)
if err != nil {
    panic(err)
}

fmt.Println(roles)

解决方案

You cannot escape backticks inside backticks, but you can do:

dbmap := db.InitDb()

var roles []entities.Role
query := `
SELECT *
FROM ` + "`Role`"

_, err := dbmap.Select(&roles, query, nil)
if err != nil {
    panic(err)
}

fmt.Println(roles)

这篇关于Golang如何逃离蜱虫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆