我在这里看到了一个非常类似的问题,但我不确定管道中的管道是做什么的,而且无论如何它都不适用于我.
所以,这是我尝试过的代码.
rows, err := db.Query(fmt.Sprintf("SELECT * FROM mytable WHERE mycolumn LIKE 'camel.%'"))
Run Code Online (Sandbox Code Playgroud)
即便如此,我也不知道它的用途是什么,我也试过过管道.
rows, err := db.Query(fmt.Sprintf("SELECT * FROM mytable WHERE mycolumn LIKE 'camel. || %'"))
Run Code Online (Sandbox Code Playgroud)
所以它应该做的是匹配以camel开头的那一列中的任何东西 camel.*
我在两个例子中得到的错误是
pq: syntax error at or near "("
Run Code Online (Sandbox Code Playgroud)
所以我猜是因为某些原因它传递了更多的那条线作为命令,而不是我想要的......也许是引用问题?我尝试过其他一些东西,但没有任何效果.任何帮助表示赞赏.
func Sprintf(格式字符串,...接口{})字符串
Sprintf根据格式说明符进行格式化并返回结果字符串.
mt.Println(fmt.Sprintf("SELECT * FROM mytable WHERE mycolumn LIKE 'camel%'"))
//Output:
//SELECT * FROM mytable WHERE mycolumn LIKE 'camel%!'(MISSING)
//Ofc postgres will complain
Run Code Online (Sandbox Code Playgroud)
fmt.Sprintf在这种情况下你不需要.
rows, err := db.Query("SELECT * FROM mytable WHERE mycolumn LIKE 'camel.%'")
Run Code Online (Sandbox Code Playgroud)
工作良好.
但如果你真的需要使用,fmt.Sprintf你必须用'%%'来逃避'%'
rows, err := db.Query(fmt.Sprintf("SELECT * FROM mytable WHERE mycolumn LIKE 'camel.%%'"))
Run Code Online (Sandbox Code Playgroud)