草庐IT

Transforms类型

全部标签

json - 如何解码不同数据类型的 JSON 数组?

我尝试解码的部分JSON有一个数组,可以包含字符串或整数。我将如何解析它?{"id":"abc","values":[1,2,3]},{"id":"def","values":["elephant","tomato","arrow"]},{//etc...}我尝试了以下方法:typeThingstruct{IDstring`json:"id"`Values[]string`json:"values,string,omitempty"`}得到如下错误:panic:json:cannotunmarshalarrayintoGostructfieldThing.valuesoftypestr

go - 如何将结构转换为基本类型,而不是 Go 中的直接类型?

我需要将未知立即类型的接口(interface)转换为已知的基本类型。例子packagemainimport("fmt""reflect")typeAstruct{foostring}typeBAfuncmain(){bS:=B{foo:"foo"}bI:=reflect.ValueOf(bS).Interface()fmt.Println(bI)aS:=bI.(A)fmt.Println(aS)}当这段代码运行时,可以理解的是,它会出现panic并显示以下消息panic:interfaceconversion:interface{}ismain.B,notmain.A在这个例子中:类

go - 类型 *url.URL 没有字段或方法 ParseRequestURI

这是我的代码:director:=func(req*http.Request){fmt.Println(req.URL)regex,_:=regexp.Compile(`^/([a-zA-Z0-9_-]+)/(\S+)$`)match:=regex.FindStringSubmatch(req.URL.Path)bucket,filename:=match[1],match[2]method:="GET"expires:=time.Now().Add(time.Second*60)signedUrl,err:=storage.SignedURL(bucket,filename,&sto

go - 按名称对导出类型进行分组

这个问题在这里已经有了答案:Ref"isnotatype"-storingatypeinastruct(1个回答)关闭4年前。我在文件中有这个:import("huru/routes/login""huru/routes/nearby""huru/routes/person""huru/routes/register""huru/routes/share")//HandlerstypeRegisterHandler=register.HandlertypeLoginHandler=login.HandlertypeNearbyHandler=nearby.HandlertypeShar

pointers - 为什么我的自定义错误类型会导致特定模式的错误?

这个问题在这里已经有了答案:MethodSets(PointervsValueReceiver)(3个答案)关闭4年前。我正在为go的接收器和指针而苦苦挣扎。我发现第4种模式会导致错误。为什么这种模式会导致错误,有什么区别?提前致谢。typeMyErrorstruct{}//OKpatternfunc(eMyError)Error()string{return"somethingbadhappened"}funcrun()error{returnMyError{}}//OKpatternfunc(eMyError)Error()string{return"somethingbadhap

go - 在 Golang 中将接口(interface)类型实现为函数类型

我创建了几种类型,包括接口(interface)://GetProfileHandlerFuncturnsafunctionwiththerightsignatureintoagetprofilehandlertypeGetProfileHandlerFuncfunc(GetProfileParams,interface{})middleware.Responder//Handleexecutingtherequestandreturningaresponsefunc(fnGetProfileHandlerFunc)Handle(paramsGetProfileParams,princ

go - 使用 DNSutil 从域中提取名称服务器 |类型 dns.RR 没有字段或方法 Hdr

我正在尝试使用GoLang从DNS记录中提取名称服务器。我遇到的问题是我无法从结构中读取字段。我正在将Response转换为JSON,这样我就可以“读取”它有哪些字段,为此我使用了以下代码:json,_:=json.Marshal(ns)fmt.Println(string(json))打印出来:{"Hdr":{"Name":"example.com.","Rrtype":2,"Class":1,"Ttl":172800,"Rdlength":16},"Ns":"ns2.example.eu."}现在,当我尝试使用以下方法从该字符串中打印出Name值时:fmt.Println(ns.H

go - 无法将 String 转换为 *net.TCPlistener 类型

我想将字符串放入net.Listener中,但出现错误:./server.go:26:23:cannotusemainServer(typeinterface{})astypestringinargumenttonet.Listen:needtypeassertion./server.go:27:28:cannotusegpServer(typeinterface{})astypestringinargumenttonet.Listen:needtypeassertion这是我的代码:viper.SetConfigFile("config.json")viper.AddConfigPa

go - 为什么在 Go 类型开关中声明一个单独的变量?

我无法理解为什么类型开关是用switch语句中定义的附加变量编写的。下面的代码似乎是被认可的做事方式:functest_func(iinterface{}){switchv:=i.(type){caseint:fmt.Printf("%T\n",v)casefloat64:fmt.Printf("%T\n",v)caseint:fmt.Printf("Idon'tknowabouttype%T!\n",v)}}funcmain(){test_func(float64(34))test_func(int(34))test_func("helloworld")}正如预期的那样,这将返回:f

go - 具有可变输入/输出类型的通用函数

只是玩awssdkforgo。当列出不同类型的资源时,我倾向于使用很多非常相似的函数,例如下面示例中的两个。有没有办法将它们重写为一个通用函数,该函数将根据作为参数传递的内容返回特定类型?类似于:funcgeneric(session,funcToCall,t,input)(interface{},error){}目前我必须这样做(功能相同,只是类型发生了变化):funcgetVolumes(s*session.Session)([]*ec2.Volume,error){client:=ec2.New(s)t:=[]*ec2.Volume{}input:=ec2.DescribeVol