草庐IT

which_case

全部标签

戈朗 :which way is more efficient about using "for range"

typepath[]bytefunc(ppath)ToUpper(){fori,b:=rangep{if'a'在上面(这个例子是从“TheGoBlog”复制过来的),如果ToUpper变成这样:func(ppath)ToUpper(){fori,_:=rangep{if'a'哪个会更有效率为什么?“TheGoBlog”对前一个说:“这里的ToUpper方法在forrange构造中使用两个变量来捕获索引和slice元素。这种形式的循环避免了在主体中多次写入p[i]。”什么意思? 最佳答案 前者有更多的内存操作,即在b上:它在循环的第一

unit-testing - httptest.NewRequest 与 http.NewRequest : which one to use in tests and why?

Golang有这两个相似的库http和httptest并且它们都有NewRequest函数。如果http.NewRequest能做到这一切,为什么我们还需要httptest.NewRequest?如果我需要为我的测试创建多部分/多形式请求,我需要使用哪一个? 最佳答案 如文档中所示,httptest.NewRequest“返回一个新的传入服务器请求,适合传递给http.Handler进行测试”,而http.NewRequest“返回适合与Client.Do或Transport.RoundTrip一起使用的请求。”因此,如果您在单元测

slice 接口(interface)上的 Golang Switch Case

是否可以区分switchcase中的[]interface{}和interface{}?尝试创建一个解码函数,您可以在其中传递不同的类型,然后switchcase确定类型,然后继续解码该特定类型。虽然当传递的类型是[]interface{}时我遇到了问题。我一直在试验reflect包,但到目前为止运气不好。请参阅下面的代码片段和Playground链接。packagemainimport("fmt""math/big")typeTeststruct{tinterface{}}funcmain(){testVar1:=big.NewInt(0)testVar2:=int64(1)test

rest - 如何使用 Gin-Gonic 在 Go 中读取 snake case JSON 请求体

我正在使用gin-gonic创建我的第一个Go休息API服务器。我的User结构如下typeUserstruct{FirstNamestring`json:"first_name"`}我的代码中定义了以下路由route.POST("/test",func(c*gin.Context){varuserrequest_parameters.Userc.BindJSON(&user)//somecodeherec.JSON(http.StatusOK,token)})我的POST请求体如下{"first_name":"JamesBond"}在这种情况下,user.FirstName的值为""

go - 使用 reflect 遍历 go struct 字段与 case map[string]interface{} 不匹配

我有一个不寻常的任务:1.将json消息解析为Gostruct2.验证JSON中的所有字段是否在特定限制内:-字符串字段长度不再固定不变-map包含的元素不超过固定数量-如果映射键的值是嵌套结构,则验证以上2条规则为此,我使用反射,然后遍历元素,并进行类型检查:-如果是int或float-无事可做-无验证-如果是字符串-验证长度(如果失败则返回)-如果map验证map长度(如果失败则返回),然后迭代map值并递归检查它们的字段是否违反string/map规则-默认(我假设这是struct嵌套的JSON结构):将其转换为接口(interface)slice并进行递归调用。问题:在JSON

go - 重写和缩短 switch case 表达式

我有这样的代码块,我只是增加计数器:switchfileInfo.RequestType{caselib.WRITE:WriteCounter+=1caselib.READ:ReadCounter+=1}在我看来,这段代码看起来不像是惯用的golang代码。能不能做得更短更“优雅”点? 最佳答案 更地道:switchfileInfo.RequestType{caselib.Write:writeCounter++caselib.Read:readCounter++} 关于go-重写和缩短

go - 在 select{case :channel} 中更改 channel

我使用Ticker定期执行任务,但在更改它时遇到了一些问题。我会在收到一些消息时将自动收报机更改为新的自动收报机并更改间隔。下面是重现此问题的示例代码:packagemainimport("fmt""time")typeAstruct{ticker*time.Ticker}func(a*A)modify(){a.ticker.Stop()a.ticker=time.NewTicker(time.Second)}funcmain(){a:=new(A)a.ticker=time.NewTicker(time.Second)gofunc(){for{select{case“现在”将只打印一

Go Error : panic: runtime error: invalid memory address or nil pointer dereference. Changing map inside a struct which is present in 另一个结构,

这个问题在这里已经有了答案:map[string]*type"invalidmemoryaddressornilpointerdereference"(1个回答)关闭3个月前。我必须结构让我们说struct1和struct2,struct2包含一个带有struct1的映射,struct1也包含一个映射,我想更改struct1中存在的映射。这是抛出一个运行时错误:panic:运行时错误:无效内存地址或零指针解引用typeFailureDatastruct{failuresInCommitsmap[string][]string}typeDetectionResultsstruct{Fai

unit-testing - 让 'go test -run <case>' 成功为 'go test' 的一般规则是什么?

我发现'gotest'PASS,但是如果我指定subtest,它会失败,这里我给一个全局变量sample,'gotest'会PASS,'gotest-runf/sample2'会失败.我想知道我应该遵循什么一般规则来防止此类问题?走吧packagemainimport"fmt"vargstringfuncf(sstring)string{g=g+sreturns+g}funcmain(){fmt.Println(f("a"))}t_test.gopackagemainimport("testing")funcTest_f(t*testing.T){tests:=[]struct{nam

go - 如何处理在 switch case 语句中返回两个对象

我正在编写类型断言辅助方法,它接受一个interface{}并返回string和map[string]interface{}.我在case语句中返回这两个对象。为什么它要求在函数结束时返回?我错过了休息时间吗?functypeAssertionHelper(rinterface{})(string,map[string]interface{}){switchg:=r.(type){casestring:returng,nilcase[]interface{}:for_,v:=rangeg{switchs:=v.(type){casestring:returns,nilcasemap[s