草庐IT

user_interface_kit

全部标签

go - 如何在 golang 中使用不同的接口(interface)在单个网页中执行多个模板?

请原谅我一个看起来很奇怪的问题。我不确定如何在一个陈述中准确地陈述我的问题。我的网页中有三个模板,页眉、布局和页脚。在模板标题中,我有一个类别下拉菜单,并且在我的go代码中有一段带有子菜单项的字符串。Categories:=[]string{"Holiday","IQ","Future"}模板头有如下html代码Categories{{range$i,$e:=.}}{{$e}}{{end}}所以当我做一个,t,err:=template.ParseFiles("template/header.html","template/index.html","template/footer.ht

go - 如何为 CRUD 模型创建通用接口(interface)?

我正尝试在go中为我希望在我的api中使用的模型创建一个通用接口(interface)。typeModelinterface{Create(interface{})(int64,error)Update(string,interface{})(error)}我有一个实现它的personModel:typePersonstruct{Idint`json:"id"`FirstNamestring`json:"firstName"`}typePersonModelstruct{Db*sql.DB}func(model*PersonModel)Create(personStructperson

GoLang 二叉树 - 使用接口(interface)和结构

我正在尝试使用GoLang中的接口(interface)和结构来创建二叉树概念我写了下面的代码packagemainimport"fmt"typenodeinterface{add(aint)getval()int}typenode_elementstruct{elementintleft*noderight*node}func(c*node_element)add(aint){c.element=a}func(c*node_element)getval()int{returnc.element}funcmain(){varsnodes=&node_element{}s.add(1)f

testing - go中接口(interface)模拟的方法

如果我有一个如下所示的handlerfunc。“模拟”或注入(inject)包装某些对象以进行测试的接口(interface)的最佳方法是什么?funcGetOrCreateUser(whttp.ResponseWriter,r*http.Request){user:=GetUserFromContext(r.Context())ifcreated:=user.GetOrCreate();created{smtp.SendEmail(...)return}else{w.Write([]byte("HelloUser!"))}}我遇到的唯一方法似乎是这样做:typeMailerinter

testing - echo c.Get ("user") 在测试环境中不起作用

我正在尝试测试基于echo框架/路由器构建的golangAPI。我有以下测试......funcTestLogout(t*testing.T){loadConfig()db:=stubDBs(t)Convey("Whenyoupostto/logout",t,func(){Convey("withavalidtoken,youshouldgetaasuccessmsgandbeloggedout",func(){e:=echo.New()e.Use(middleware.JWTWithConfig(middleware.JWTConfig{SigningKey:[]byte("secr

golang - 将 [ ] 接口(interface)转换为 [ ] 字符串或连接字符串

如何将[]interface转换为[]strings或将其转换为包含[]interface中所有元素的连接的单个字符串?下面是显示“scope”的精确值的屏幕截图,[]interface类型的长度为2。在下面的代码中,case是“slice”,目前我正在使用reflect进行此操作,但这看起来不太好。想知道应该有一些好的方法来连接界面元素的slice\数组。我也试过像这样使用jsonunmarshall"json.Unmarshal([]byte(jwtResp),&mymap)"但在将jwt.MapClaims转换为byte[]时遇到类型问题parsedkey,_:=jwt.Pars

Golang 重映射接口(interface) go-cache

我有如下结构:typePagestruct{titlestringurlstring}和结构图:varmostViewed=make(map[int]Page)使用go-cache,我用TTL时间存储map。c.Set("data",mostViewed,60*time.Minute)但是,一旦我恢复了“数据”key,我如何才能将它返回给map呢?a,_:=c.Get("data")fmt.Printf("%+v\n",a)out:map[17:{title:xxx,url:yyy}]我试过类似的东西:z:=map[int]Page{a}有什么线索吗?这就像“重新映射”映射的字符串。

go - 如何在golang中调用接受接口(interface) slice 作为参数的方法?

这个问题在这里已经有了答案:sliceofstruct!=sliceofinterfaceitimplements?(6个答案)关闭5年前。我想在golang中调用一个接受接口(interface)slice作为参数的方法,但我发现我不能像这样传递它:typeBaseinterface{Run()}typeAstruct{namestring}func(a*A)Run(){fmt.Printf("%sisrunning\n",a.name)}funcfoo1(bBase){b.Run()}funcfoo2(s[]Base){for_,b:=ranges{b.Run()}}funcTes

go - 隐式接口(interface)调用函数

在Golang中寻找一些使用net/http包服务器静态文件的示例,我找到了实现FileSystem接口(interface)的Dir类型。一些示例显示您可以使用以下服务器静态文件:http.Handle("/",http.FileServer(http.Dir("/tmp")))http.Dir("/tmp")到底是什么?它看起来像是FileSystem的构造函数。 最佳答案 http.Dir("/tmp")实际上是一种类型转换,您将字符串/tmp转换为http.Dir类型。看着docs,你会看到http.Dir其实是一个字符串类

Go - 接口(interface)转换[恢复] - 错误

测试日志显示如下错误row0-gotdataoftypegraph.Nodebutwantedgraph.Node---FAIL:TestAlls(0.84s)panic:interfaceconversion:interface{}isgraph.Node,notgraph.Node[recovered]panic:interfaceconversion:interface{}isgraph.Node,notgraph.Node来自以下代码nnn=graph.Node{}nnn,ok=row[0].(graph.Node)if!ok{log.Printf("row0-gotdatao