Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
戈朗+;mongodb:清理输入_Mongodb_Go_Trim - Fatal编程技术网

戈朗+;mongodb:清理输入

戈朗+;mongodb:清理输入,mongodb,go,trim,Mongodb,Go,Trim,我想在将用户的输入插入数据库之前验证它。使用此微调功能,我可以检查输入是否为空或是否为空 if len(strings.Trim(r.Form.Get("firstName"), " ")) == 0 { 但我还想检测以下信息: "username": {"$ne": null} 我尝试了一些的东西,比如if len(strings.Trim(r.Form.Get(“firstName”),“$/^\”)==0{,但总是在符号方面出错。我做错了什么?为了消除错误,你需要避开反斜杠 主程序包

我想在将用户的输入插入数据库之前验证它。使用此微调功能,我可以检查输入是否为空或是否为空

if len(strings.Trim(r.Form.Get("firstName"), " ")) == 0 {
但我还想检测以下信息:

"username": {"$ne": null}

我尝试了一些
的东西,比如if len(strings.Trim(r.Form.Get(“firstName”),“$/^\”)==0{
,但总是在符号方面出错。我做错了什么?

为了消除错误,你需要避开反斜杠

主程序包
进口(
“fmt”
“字符串”
)
func main(){
名字:=“^$/Hans\\\^$”
sanitizedName:=strings.Trim(firstName,$/^\\”)
fmt.Println(净化名称)
}
输出:

Hans

首先,使用
strings.TrimSpace
来修剪空白似乎更好,因为它涵盖了更多的情况。其次,当您谈到清理这样的消息时,不清楚您的意思是什么:
“username”:{“$ne”:null}
。是否要删除所有类似
“:{$}的字符
并将其设置为
username ne null
?但在这种情况下,长度仍然大于0。此外,您可以使用原始字符串文字```使转义字符变得不必要。