草庐IT

TabList的创建

全部标签

go - 使用 initilzer 函数创建的结构不会导出到测试包中吗?

假设我们有一个struct和一个类似这样的结构的构造函数packagemyPackagetypeClientstruct{aTypeAbTypeB}funcNewClient(aTypeA,bTypeB)ConcreteClient{return&Client{a:a,b:b,}}typeConcreteClientinterface{ExportedFunc()}func(c*Client)privateFunc(){//...}func(c*Client)ExportedFunc(){//...}我们在这样的测试包中使用这个客户端var(c=&Client{a:a,b:b,})fu

json - 创建 JSON 数组和 JSON 对象

我正在为Go开发一个应用程序。该应用正在从第三方获取JSON数据。我不知道如何创建json对象并将其附加到json数组。发生的事情是我有一个循环,每个元素都收到了。下面是代码:typePastWeekWeatherArraystruct{PastWeekWeather[]PastDayWeather`json:"array"`}typePastDayWeatherstruct{DayWeatherstring`json:"day"`}funcget_weather(ctxcontext.Context,placestring,datestring)(string,error){varm

go - 运行由 Go build 创建的二进制文件时出现问题

我有一个简单的Go应用程序,它有一些模板文件,我可以在其中呈现一些文本。在使用Gobuild构建我的二进制文件后,我尝试运行该文件,但出现错误:panic:html/template:patternmatchesnofiles:public/*.html我正在使用Echo框架并按照他们的步骤为模板添加渲染。这是我的main.go文件中的代码//TemplateRendererisacustomhtml/templaterendererforEchoframeworktypeTemplateRendererstruct{templates*template.Template}//Rend

go - 我如何在 golang 中反射性地创建一个枚举实例?

我正在尝试根据其reflect.Type和值创建一个枚举实例https://play.golang.org/p/PqklMe_Z4WXpackagemainimport("fmt""reflect")typeWeekDaystringconst(SUNDAYWeekDay="sunday"MONDAYWeekDay="monday")func(dayWeekDay)WeekDay()bool{switchday{caseSUNDAY,MONDAY:returntruedefault:returnfalse}}funcmain(){rt:=reflect.TypeOf(WeekDay("

go - 在golang中动态创建结构数组

我尝试创建一个接受任何结构值的通用函数并创建该结构类型的数组。这是我试过的代码。但我收到错误“t不是一种类型”。我该如何实现。typeRegAppDBstruct{nmstringdata[]interface{}}funcCreateRegTable(tblstring,recinterface{})RegAppDB{t:=reflect.TypeOf(rec)fmt.Println(t)returnRegAppDB{"log",[]t}} 最佳答案 Go不支持泛型,任何类似的尝试都不会成功。在您的具体情况下,存在几个关键问题:您

go - 创建一个函数来测试从接口(interface)编码/解码

我想创建一个简单的函数来测试编码/解码记录是否按预期工作。我只是在这个例子中使用JSON:packagetestimport("encoding/json""fmt""testing""reflect""github.com/stretchr/testify/require")funcCheckRoundTripJSON(t*testing.T,recordinterface{}){data,err:=json.Marshal(record)require.NoError(t,err)fmt.Println("Record:",record,"wasencodedto:",data,"

variables - 如何通过连接两个字符串来创建动态变量名

我想通过连接当前日期来创建变量名,并为创建的变量名提供一个值。我的变量名应该类似于这样的"Key-2019-01"这样我就可以将值存储为varKey-2019-01="yes"我试过如下。packagemainimport("fmt""time""strconv""strings")funcmain(){currentMonth:=time.Now().Month()currentYear:=time.Now().Year()varmonth=int(currentMonth)varcurrentDate=strings.Join([]string{strconv.Itoa(curre

go - 成对迭代字符串数组以创建映射

这个问题在这里已经有了答案:Golang:convertslicesintomap(7个答案)关闭3年前。我有一个字符串数组,其长度始终是二的倍数。我想用这个数组创建一个map,这样myarr=["key1","val1","key2","val2",...]成为一张map,其中mymap:=mapify(myarr)mymap['key1']=="val1"mymap['key2']=="val2"在Python中我可以用下面的代码做到这一点mymap={}forx,yinzip(*[iter(myarr)]*2):mymap[x]=y

json - 尝试解析 JSON 并创建提取的 JSON

我正在尝试根据从API接收到的数据动态创建一个JSON对象。收到的示例数据:将数据解码到下面给出的CiItems结构中{"class_name":"test","configuration_items":[{"id":"ea09a24f-01ef-42ad-ab19-e0369341d9b3","ci_name":"makk","comments":null,"created_by":"mike","updated_by":"sam","created":"2019-08-02T21:16:35.656Z","updated":"2019-08-02T21:21:08.073Z","c

go - 创建路由模块 Go/Echo RestAPI

我刚开始学习Go,想创建自己的RESTAPI。问题很简单:我想将我的api的路由放在不同的文件中,例如:routes/users.go,然后我将其包含在“main”函数中并注册这些路由。在Echo/Go中有大量的restAPI示例,但它们都在main()函数中有路由。我检查了一些示例/github入门工具包,但似乎找不到我喜欢的解决方案。funcmain(){e:=echo.New()e.GET("/",func(cecho.Context)error{responseJSON:=&JSResp{Msg:"HelloWorld!"}returnc.JSON(http.StatusOK,