草庐IT

validates_uniqueness_of

全部标签

validation - Golang 验证器多字段依赖

我想验证以下结构:typeCarModelstruct{gorm.ModelOwnerIDint`json:"ownerid"validate:"nonzero"`Typestring`json:"type"validate:"regexp=(?)(A|B)"`Astring`json:"url"validate:"isurl"`Bstring`json:"ip"validate:"isip"`}我想根据类型验证A和B,如果type=A那么A必须存在并且必须是一个URLBUT不能存在如果type=B那么A一定不存在并且B必须是一个IP验证器可以吗?我确实尝试过自定义验证,但我找不到查看

regex - Go validator.v2 为正则表达式给出错误 "unknown tag"

我一直在努力理解为什么一些正则表达式在使用validator.v2时给我一个错误“Unknowntag”在golang中打包。它适用于某些正则表达式,但不适用于其中包含“{}”的某些正则表达式,并且当我使用validator.Validate()时,它在运行时给我一个错误“未知标记”。代码如下:typeCompanystruct{Namestring`validate:"regexp=^[a-zA-Z.]{1,100}$"`}这在运行时给我以下错误:Name:unknowntag但是这个正则表达式工作得很好typeCompanystruct{Namestring`validate:"r

go - panic : runtime error: slice bounds out of range when concurrently running as goroutine

我将一个函数作为goroutine调用,并使用WaitGroup来防止在它们全部完成之前关闭共享扫描仪。myfunc()函数迭代一个文件。我想内存映射这个文件并在所有goroutine之间共享它,而不是每次都从磁盘读取I/O瓶颈。有人告诉我这种方法可行inananswertoanotherquestion.然而,虽然这个函数独立运行良好,但它不能同时运行。我收到错误:panic:runtimeerror:sliceboundsoutofrange但错误是当我调用Scan()方法时(不在slice上),这令人困惑。这是一个MWE://...packagedeclaration;impor

unit-testing - 如何编写When a function having the parameters of c *gin.Context 的测试用例

我正在用golang为我的项目编写Controller的测试用例。在Controller中有函数名称SaveProvider()有参数c*gin.Context我不知道如何将JSON传递给c*gin.Context这个参数以及我如何测试我在Controller中使用的函数谁能告诉我这段代码中的问题是什么。它也称为表驱动测试。packagecontrollersimport("bkapiv1/models""fmt""testing""github.com/gin-gonic/gin")funcTestSaveProvider(t*testing.T){typeargsstruct{c*

Elasticsearch 查询 : Select documents by comparing lists of values (golang)

我有一种在ElasticSearch中索引的文档,其简化结构如下:{id:"54"properties:["nice","green","small","dry"]}现在我想选择该索引中的所有文档,这些文档不在properties字段中包含给定值的列表。类似于:SELECT*FROMindexWHEREpropertiesNOTCONTAINS["red","big","scary"]我如何在elasticsearch上实现它?(而且我有人知道如何在Golang上实现这样的查询,我会做得更好:-))谢谢! 最佳答案 您可以使用子句b

go - 包 github.com/matcornic/hermes/v2 : cannot find package "github.com/matcornic/hermes/v2" in any of:

我想使用Golang电子邮件模板当我运行时goget-ugithub.com/matcornic/hermes/v2itreturnspackagegithub.com/matcornic/hermes/v2:cannotfindpackage"github.com/matcornic/hermes/v2"inanyof:/usr/local/go/src/github.com/matcornic/hermes/v2(from$GOROOT)/home/User/go/src/github.com/matcornic/hermes/v2(from$GOPATH)我的go变量GOPATH

validation - 如何在 Go 中处理控制台输入类型验证?

我正在尝试使用循环为程序构建一个非常基本的控制台输入。但是,当用户输入的不是整数时,错误消息的触发次数与输入字符串中的字符数(包括换行符)一样多。我已经尝试过使用Scan()、Scanln()和bufio.NewReader()进行字符串解析,并在Println()之后使用continue。所有结果都相同。varthreadsintfuncmain(){fmt.Println("Enternumberofthreads:")for{_,err:=fmt.Scanln(&threads)iferr!=nil{fmt.Println("Enteravalidnumber")}else{br

go - 如何修复 VS Code 错误 : "Not able to determine import path of current package by using cwd" for Go project

我正在学习教程,我想我可能错过了一些东西。我有一个Go项目位于:/Users/just_me/development/testing/golang/example_server内容是:main.gopackagemainimport"fmt"funcmain(){fmt.Println("hiworld")}我有一个~/go目录。goenv显示:GOPATH="/Users/just_me/go"GOROOT="/usr/local/Cellar/go/1.12.9/libexec"我在VSCode中安装了建议的包。当我保存我的main.go时,我得到:Notabletodetermi

go - 使用 reflect 设置 struct of struct values 的值

我有一些看起来可以工作但最终什么也没做的代码:http://play.golang.org/p/TfAWWy4-R8有一个结构,该结构具有结构类型的字段。内部结构具有所有字符串字段。在循环中使用反射,想要从外部结构中获取所有结构字段。接下来,填充内部结构中的所有字符串值。示例代码从标签中获取文本并在“,”上对其进行解析,以获取内部循环的字符串值。这是应该创建内部结构并将解析的数据添加到字符串值的主要部分。t:=reflect.TypeOf(Alias{})alias=reflect.New(t)fori:=0;i当您查看示例的输出时,它看起来像是在工作,但是在从外部结构打印一个值之后,

json - 无法将字符串解码到 Go struct 字段 Article.article_type of type models.ArticleType

我无法将json字段article_type解码为golang结构Article。我遇到错误:json:无法将字符串解码到Gostruct字段Article.article_typeoftypemodels.ArticleTypestr:=[]byte(`[{"created_at":1486579331,"updated_at":1486579331,"article_type":"news"}]`)typeArticlestruct{IDuint`gorm:"primary_key"`CreatedAttimestamp.Timestamp`json:"created_at"`Up