草庐IT

test_slice

全部标签

go - 将 slice 中的单个字符串替换为一组字符串

我想要一段字符串,当找到某些字符串时,将它们替换为一组相关的字符串。例如,如果我有这个:[]string{"A","FROM_B_TO_E","F"}在我的方法运行后我想要:[]string{"A","B","C","D","E","F"}我想出了这段代码,问题是,虽然我可以在实际添加之前打印我的to_be_addedslice,但由于某种原因它不起作用。它确实有效,但是如果我更改我的translateRule所以它不会返回一段字符串,而是只返回一个字符串:funcgroupRules(validationRules[]string){forindex,rulename:=rangev

go - 将结构放入 slice 时出现 golang 错误

完整代码:packagemainimport("net")vararrayOfConnections[]*ConnectiontypeConnectionstruct{connnet.Connidint}funcmain(){serv,_:=net.Listen("tcp",":8080")deferserv.Close()connectionHandle(serv)}funcconnectionHandle(servnet.Listener){conn,_:=serv.Accept()arrayOfConnections=append(&Connection{conn,1},0)co

go - 在go中将Slice指针转换为Slice

我有一些方法可以返回slice指针中的数据,现在我必须将其转换为slice数组。如何将slice指针转换为slice数组。peerRoundState,err:=s.nodeview.PeerRoundStates()fmt.Println("Thisreturnvalueisslicepointer",peerRoundState)iferr!=nil{returnnil,err}//PeerRoundStatesthisistypeofslice.return&ConsensusResponse{RoundState:s.nodeview.RoundState().RoundSta

go - 为特定包运行 go test

我使用以下命令对特定包运行测试去测试fts-runrun_test.gocan'tloadpackage:packagefts:cannotfindpackage"fts"inanyof:/usr/local/Cellar/go/1.11.1/libexec/src/integration(from$GOROOT)/Users/i055555/go/src/fts(from$GOPATH)包裹看起来像gitproj/|----fts|-----command|-----run.go|-----run_test.go|----internal|-----fs.go|-----tb.go|

unit-testing - 当A的方法在Go中返回B时模拟对象A和B

我正在尝试在Go中为现有服务实现单元测试,该服务使用连接池结构和来自现有库的连接结构(调用这些LibraryPool和LibraryConnection)连接到外部服务。为了使用这些,主代码中的服务函数使用池的一个唯一的全局实例,它有一个GetConnection()方法,如下所示://CurrentMainCodevarpoolLibraryPool//global,instantiatedinmain()funcsomeServiceFunction(whttp.ResponseWriter,r*http.Request){//readrequest//...conn:=pool.

go - 重新分配 slice 参数的行为不同

packagemainimport"fmt"funcmain(){paths:=[]string{"hello","world","mars"}varresult=delete(paths,1)fmt.Println(result)fmt.Println(paths)}funcdelete(paths[]string,indexint)[]string{paths=append(paths[:index],paths[index+1:]...)returnpaths}上面代码的结果如下:[hellomars][hellomarsmars]如您所见,第二个fmt.Println(path

go - 对切​​片内的整数 slice 进行排序

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭3年前。Improvethisquestion我正在尝试对slice内的intslice进行排序,例如:slices:=make([][]int32,0)slices=append(slices,[]int32{10,22})slices=append(slices,[]int32{13,22})s

postgresql - 如何创建一个在列中包含 slice 的表

我有一个看起来像这样的模型:typeInventorystruct{gorm.ModelLocationIDstringItems[]Item//thisisasliceofstructsCategories[]Category//thisisasliceofstructs}当我使用gorm为它创建一个表时,我没有项目或类别的列。我错过了什么? 最佳答案 因为数组在SQL中不支持列类型——至少大多数版本的SQL是这样——gorm不会为slice类型的字段创建列。但是,您可以在使用关联后创建您现在的关系结构。在这种情况下,has-ma

go - "go test"和 "bazel test"中的不同文件权限错误

如果一个测试想要断言文件权限错误,例如,写入文件系统的根目录,“gotest”返回一个syscall.EACCES错误,而“bazeltest”返回一个系统调用.EPERM。如何让“bazeltest”和“gotest”都通过测试?可以找到一个例子here. 最佳答案 您可以使用bazel--spawn_strategy=standalonetest//...禁用沙箱。我怀疑这会解决这个问题。但是,您可能需要考虑写入/是否是您要测试的行为。如果您需要在不同的操作系统或Docker容器内运行代码,在这种情况下您将获得不同的行为,因此您

unit-testing - 如何修复测试用例中的 "missing type in composite literal"

我正在尝试为函数ReadField()编写测试代码,但我在定义测试用例时遇到了困难。它给出了一个错误“复合文字中缺少类型”。我相信这只是一些语法错误。我已经尝试在函数体之外定义结构体,但它仍然会给出相同的错误。ReadField(string,string,bool)(bool,string)funcTestReadField(t*testing.T){testCases:=[]struct{NamestringInputstruct{FirstStringstringSecondStringstringSomeBoolbool}Expectedstruct{IsValidboolMe