草庐IT

functional-interface

全部标签

go - 如何从另一个函数调用 dbmap.Insert(interface{})?

我有一堆非常相似的结构(示例中的A和B),我想在某些函数(示例中的f())中处理它们的实例,然后将它们插入到我的数据库中.我想我可以以某种方式用空接口(interface)处理它,但似乎这不是解决方案,因为我收到错误:i:&{{62019-04-0315:11:37.822100431+0200CESTm=+0.001291882}7}*main.A2019/04/0315:11:37Insertinotablefoundfortype:exitstatus1我尝试创建一些最小但可执行的示例:packagemainimport("database/sql""fmt"_"github.c

go - 使用接口(interface)设置具体结构的值

我正在尝试在具体结构中设置属性的值,使用该结构使用和接口(interface)实现的方法。该结构还由另一个结构组成。在下面的示例中,userId值仍然是“aaaa”并且不会更新为“userid1”。如何通过方法接口(interface)作为参数并通过结构体作为参数来更新结构体的值?funcmain(){user:=User{}mw:=SomeMiddleware{}user.UserId="aaaa"mw.Process(user)fmt.Println("UserId:"+user.UserId)}typeIUserinterface{SetUserId(string)}typeSo

go - 我如何模拟接口(interface)内的特定嵌入式方法

我有这段代码,我想为更新功能编写一个单元测试。我如何模拟FindByUsername函数?我尝试覆盖u.FindByUsername但它不起作用。另外,我可以编写一些函数来将u*UserLogic和userNamestring作为输入参数并执行u.FindByUsername()和模拟这个函数,但这不是一个干净的解决方案我需要一个更好的解决方案来模拟UserOperation接口(interface)中的方法。packagelogicimport("errors""fmt")var(dataStore=map[string]*User{"optic":&User{Username:"b

go - 如何在没有指数的情况下从 json 的 map[string]interface{} 格式化 int 数字?

此演示:https://play.golang.org/p/7tpQNlNkHgGpackagemainimport("fmt""encoding/json")funcmain(){jsonStr:=`{"code1":10080061,"code2":12.2}`data:=map[string]interface{}{}json.Unmarshal([]byte(jsonStr),&data)fork,v:=rangedata{fmt.Printf("%v:%v,%v:%f,%v:%.0f\n",k,v,k,v,k,v)}}输出:code1:1.0080061e+07,code1:

go - 获取从 PubSub 事件触发的 Google Cloud Functions 的执行 ID

对于从HTTP触发的GoogleCloudFunctions,可以通过检查HTTP请求的header(“Function-Execution-Id”)来检索执行ID:packagepimport("fmt""net/http")funcF(whttp.ResponseWriter,r*http.Request){executionID:=r.Header.Get("Function-Execution-Id")fmt.Println(executionID)}但是,对于由PubSub事件触发的GCF,我找不到如何检索此执行ID:packagepimport("context")type

function - 在 Go 中是否可以调用带有命名参数的函数?

这个问题在这里已经有了答案:Initializefunctionfields(2个答案)关闭3年前。我想在Go中调用一个函数,并将参数名称附加到参数值funcsum(aint,bint)int{returna+b}funcmain(){result:=sum(a=4,b=5)//result==9}这可能吗?

go - 如果传入的数组为 &val 然后转换为接口(interface){},则更新数组元素

我正在尝试编写一些通用方法(CRUD方法)以在我的服务之间共享它。以下示例是一个GetAll()方法,它返回我的集合中存在的所有文档:funcGetAll(outinterface{})error{//mongodboperations//iteratethroughalldocumentsforcursor.Next(ctx){variteminterface{}//decodethedocumentiferr:=cursor.Decode(&item);err!=nil{returnerr}(*out)=append((*out),item)//arrays.AppendToArr

json - 在 map[string]interface{} 的值上键入 switch 到 []map[string]interface{}

问题我面临从json对象中删除不需要的数组的问题,例如。只有一个元素不是对象或数组的数组。(没有数组作为输入的根)例子在:{"name":[{"inner":["test"]}]}通缉令:{"name":[{"inner":"test"}]}方法我从对已解析的map[string]interface{}的值进行简单类型切换开始,并认识到它不会切换到case[]map[string]interface{}。(举个例子)这是我想出的实现。它适用于大多数场景,但不适用于数组中的内部对象。typejsonMapmap[string]interface{}typejsonMapList[]map

go - 如何将嵌套接口(interface)转换为 map slice 的 map ?

我有一个map数组的mapmap[string][]map[string]string,只是,当我获取数据时,它的格式是map[interface{}]map[interface{}][]map[interface{}]interface{},所以我只剩下做一堆嵌套的类型断言,这真的很笨拙,需要很长时间来编写,是难以读/写,并且可能容易出错,就像这样;ifkey=="identities"{idErrMessage:="Sorry,therewasaproblemwithanidentity"idArray,ok:=setting.(map[string]interface{})ifo

user-interface - 增加这个简单的 go gui 应用程序的默认字体大小

我正在使用以下简单的演示代码来使用Fyne创建一个GUI包:packagemainimport("fyne.io/fyne/widget""fyne.io/fyne/app")funcmain(){app:=app.New()w:=app.NewWindow("Hello")w.SetContent(widget.NewVBox(widget.NewLabel("HelloFyne!"),widget.NewButton("Quit",func(){app.Quit()}),))w.ShowAndRun()}一切正常,但我想增加此GUI的默认字体大小(以便标签、按钮和任何其他小部件(如