草庐IT

go-mocket

全部标签

go - 如何更新 golang 时间的小时、分钟、秒?

例如:如何在t时间更新小时数?fmt.Println(t)//=>2006-01-0215:04:05+0000UTC期望得到:2006-01-0200:00:00+0000UTC编辑:类似于:time.TimeRoundtoDay 最佳答案 使用:t1:=time.Date(t.Year(),t.Month(),t.Day(),0,0,0,t.Nanosecond(),t.Location())引用:https://golang.org/pkg/time/#Date 关于go-如何更新

go - 调用通过插件导入的结构的函数

我有以下插件:packagemaintypeTeststruct{Idstring}func(test*Test)GetId()string{returntest.Id}varVTest我正在我的应用中导入它:packagemainimport("fmt""plugin")funcmain(){p,err:=plugin.Open("test.so")iferr!=nil{panic(err)}v,err:=p.Lookup("V")iferr!=nil{panic(err)}fmt.Println(v)}不幸的是,我无法对其调用v.getId()-有没有办法公开在给定结构上设置的所有

go - 如何取消转义html字符串中的引号

我在Go中有一个字符串如下:Helloworld!Google引号被转义了,我想得到没有反斜杠的字符串。我尝试使用html.UnescapeString但不是我想要的。我的问题有解决办法吗? 最佳答案 使用strings.NewReplacer()funcNewReplacer(oldnew...string)*Replacerpackagemainimport("bytes""fmt""log""strings""golang.org/x/net/html")funcmain(){consthtm=`Helloworld!Goog

go - 当 channel 关闭时,以接收 channel 作为参数的 goroutines 是否停止?

一直在看《用go构建微服务》,书中介绍了apache/go-resiliency/deadline用于处理超时的包。deadline.go//Packagedeadlineimplementsthedeadline(alsoknownas"timeout")resiliencypatternforGo.packagedeadlineimport("errors""time")//ErrTimedOutistheerrorreturnedfromRunwhenthedeadlineexpires.varErrTimedOut=errors.New("timedoutwaitingforf

go - append 的第一个参数必须是 slice(struct 和 golang)

你好,我是golang的初学者,我正在开发购物车应用程序,每次我尝试执行代码时,它都会显示“要附加的第一个参数必须是slice”这是代码打包车typeCartstruct{itemsmap[string]Item}typeItemstruct{idstringnamestringpricefloat32quantityint}funcNewItem(istring,nstring,pfloat32)Item{returnItem{id:i,name:n,price:p,quantity:0,}}funcNewCart()Cart{returnCart{items:map[string]

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,"

go - 如何比较 Int Var 与结构的内容

使用Go1.11。得到以下结构,typeinputsDatastruct{TurnOneintTurnTwointTurnThreeintTurnFourintTurnFiveintTurnSixintTurnSevenintTurnEightintTurnNineint}保存用户输入的数据。没问题,但我想从TurnTwo检查用户输入的内容是否已经输入?示例,输入1->6,输入2->4,输入3->6所以一旦用户再次为第3轮输入6,它会记录,抱歉,再试一次...这就是我检查的方式,funccheckInputTwo(userInputint)bool{varokboolok=trueif

go - 如何使 template.Execute() 写入文件而不是 response.Writer?

我有下面的代码来解析模板文件并将解析后的html写入ResponseWriter:-packagemainimport("net/http""html/template")funchandler(whttp.ResponseWriter,r*http.Request){t,_:=template.ParseFiles("view.html")t.Execute(w,"HelloWorld!")}funcmain(){server:=http.Server{Addr:"127.0.0.1:8080",}http.HandleFunc("/view",handler)server.List

go - 结构的一个字段,由 3 个项目组成。这怎么编译?

这个问题在这里已经有了答案:StrangetypedefinitionsyntaxinGolang(name,thentype,thenstringliteral)(1个回答)Whatistheusageofbacktickingolangstructsdefinition?[duplicate](2个回答)WhatisthethirdparameterofaGostructfield?(2个回答)GoStringaftervariabledeclaration(2个回答)StringliteralsinGOstructuredefinition[duplicate](1个回答)关闭3

go - 如何覆盖重载变量

我正在试验gowithpi计算公式,并做了这个:packagemainimport("fmt""math")funcbbpformula(){varresultfloat64fork:=0.0;k输出是:3.141592653589793但是,我想要更多的数字,我尝试使用math/big包使变量变大。Float具有更简单的公式,但算术运算非常困惑且缓慢。所以我的想法是在结果变量过载(最大容量)时覆盖它,但我不知道该怎么做!有人可以向我解释一下这是怎么做到的吗?编辑:尝试使用ifresult>math.MaxFloat64但输出变为空import("fmt""math")funcbbpf