草庐IT

Transforms类型

全部标签

inheritance - 从没有将类型转换为继承类型的继承结构执行实际对象方法

packagemainimport("fmt")typeIAinterface{Parse()Name()string}typeAstruct{IA}func(a*A)Name()string{return"AName"}func(a*A)Parse(){fmt.Println("A-"+a.Name())}typeBstruct{A}func(b*B)Name()string{return"BName"}funcmain(){a:=&A{}b:=&B{}a.Parse()b.Parse()//Iwouldliketosee"A-BName"}Playground当我从继承结构执行方法

json - 将 JSON 解码为结构,同时支持属性值的不同类型

在不同情况下,Golang是否有可能将JSON对象解码为结构,其中JSON对象具有可以是对象或数组(或通常支持各种类型)的属性?例如,在一种情况下,JSON可能如下所示:{"config":{"source":"config.cnf"}}但同时,JSON也可能是这样的:{"config":["value1","value2"]}如果是这样,结构会是什么样子? 最佳答案 您应该解码为空接口(interface)(interface{})。因为它没有方法,所以每个类型都实现它。typeDatastruct{Configinterface

go - 带接收器的函数类型

我如何声明一个带有接收者类型的函数?我以为我可以执行以下操作,但它提示语法错误:typemyFuncfunc(s*State)(blahBlah)errfuncmain(){b:=&Blah{}s:=&State{}varf=myFs.f(b)}func(s*State)myF(blahBlah)err{...} 最佳答案 您可以定义一个将接收者作为其第一个参数的函数类型(本质上就是方法)。typemyFuncfunc(*State,Blah)error然后您可以使用methodexpression创建该类型的值:typeBlahs

go - 动态函数返回类型

我有一个按模块分开的应用程序。有几个实体和CSV模块。Csv模块仅支持结构(实体),但我想让CSV模块适用于任何类型的实体。现在它是这样工作的:Csv模块从channel接收数据并将其严格转换为EverySize结构。我怎样才能实现动态返回类型,以便它适用于任何类型的实体,而不仅仅是EverysizefuncprepareWrapData(data[]feed.WrapExporterChannels)[]everysize.EverySizeItem{varresult[]everysize.EverySizeItemfor_,value:=rangedata{result=appe

go - 将空接口(interface)转换为 Golang 中的等效类型

将动态接口(interface)转换为其等效类型。例如,如果值是int,它应该返回int,如果是string,那么它应该返回int。代码示例:varoptions=bson.M{}for_,val:=rangeconditions{varattr,operator,valueinterface{}cons:=val.(map[interface{}]interface{})forrangecons{attr=cons["attribute"]operator=cons["operator"]value=cons["value"]switchoperator{case"==":opera

go - 未找到类型 InteractionCallback,atom 正在删除导入语句

我正在使用atomIDE,出于某种原因,每当我将其添加到我的导入中时:“github.com/nlopes/slack”并保存文件,它会删除导入。所以我不确定为什么在库中找不到InteractionCallback类型?我从example复制了这段代码:funcunmarshalSuggestionCallback(jstring)(*InteractionCallback,error){callback:=&InteractionCallback{}iferr:=json.Unmarshal([]byte(j),&callback);err!=nil{returnnil,err}re

json - 将 JSON 文件读入自定义类型结构,我需要在需要类型字符串而不是自定义类型的函数中使用键

我正在创建一个工具,它可以获取JSON文件,然后使用Go从中创建PDF这是我的JSON示例:[{"Name":"Ollie","Age":"25","Comment":"Thisismycomment"},{"Name":"Amy","Age":"28","Comment":"Anothercomment"},{"Name":"Joey","Age":"19","Comment":"CommentfromJoey"},{"Name":"James","Age":"23","Comment":"James'comment"},{"Name":"Richard","Age":"20","C

regex - 如何删除字符串中的空格或制作接受所有类型有效电子邮件的正则表达式

我有一个由","分隔的字符串,例如string:="abc@bk.com,cde@bk.com"我想制作一个正则表达式,它将覆盖电子邮件前后的所有空格,或者是否有另一个函数strings.Replace来替换空格?他们都做同样的工作,但我不知道哪个更好。如果正则表达式更好,那么您可以举个例子,如果strings.Replace函数更好,那么请提供一个例子。我已经尝试了一个小代码:-packagemainimport("fmt""regexp""strings")typeUserstruct{Name[]CustomerDetails`json:"name"bson:"name"`}ty

elasticsearch - 没有在 Field ElasticSearch 6.4.3 上声明类型关键字的处理程序

请问这是什么原因。这是代码packagemainimport("context""errors""fmt""time""github.com/olivere/elastic")const(indexName="applications"docType="log"appName="myApp"indexMapping=`{"mappings":{"log":{"properties":{"app":{"type":"keyword"},"message":{"type":"keyowrd"},"time":{"type":"date"}}}}}`)typeLogstruct{Appstr

go - 在 Go 中使用 color.RGBA 结构类型创建新颜色时,我收到错误消息,指出整数格式错误

我正在尝试使用创建一个调色板varpalette=[]color.Color{color.RGBA{0xRR,0xGG,0xBB,0xff},color.Black}但是我收到了这个错误:./lissajous.go:13:40:malformedintegerconstant:0x./lissajous.go:13:42:malformedhexconstant./lissajous.go:13:42:syntaxerror:unexpectedRR,expectingcommaor} 最佳答案 原始代码中的无效值0xRR、0XG