草庐IT

load_and_store_funcs

全部标签

go - 如何解决 `unknown escape sequence (and 2 more errors)`的错误

我正在尝试使用golang代码验证图像url,但正则表达式有错误我在这个问题中显示了我的正则表达式:-varvalidation=regexp.MustCompile("(http(s?):)|([/|.|\w|\s])*\.(?:jpg|gif|png)")错误:-unknownescapesequence(and2moreerrors)playlink 最佳答案 \.是无效的转义序列。我建议您在定义正则表达式时使用反引号。例如regexp.MustCompile(`^https?://.*\.(jpg|gif|png)$`)//

json - 如何使用 Golang func json.Unmarshal 解码这个复杂的 Json 数据?

我有这样的json对象:{"action":"GetLoad","resource_id":"lb-cdvyel0v","ret_code":0,"meter_set":[{"data_set":[{"data":[[1478672400,[1,0]],[1,0],[0,0],[8,0],[1,0]],"eip_id":"eip-jf79ljt7"},{"data":[[1478693280,[0,0]],[1,0],[0,0]],"eip_id":"eip-mw6n6wg0"}],"meter_id":"uaffic"}]}我尝试这样解决问题:typeCommonResponsest

json - Go 和 JSON : how to dynamically load a field

我知道如何在go中使用JSON和接口(interface)而没有太多问题。我想让用户从JSON字符串中选择一个JSON元素,并将元素模式存储在一个字符串中,以便我以后可以动态加载它。我有以下JSON:{"id":1,"name":"Agreendoor","price":12.50,"tags":["home","green"]}当然,如果我想要我的JSON的id元素,这很容易,因为id是我要保存的字符串。现在假设我想要标签[1]。随着JSON变得越来越复杂,您会发现这变得越来越难。例如,我可能想保存类似于tags[1].data[0].values.id等的模式......基本上,我

Go Lang- Gin : How to extract only the body (and ignore other garbage) from httputil. DumpRequest

我知道你可以从ioutil.ReadAll(c.Request.Body)但是使用httputil.DumpRequest转储,错误:=httputil.DumpRequest(c.Request,true)将给出正文内容以及其他值,最后是正文内容。Contenttype:application/jsonIP:127.0.0.1:36846headertoken:Contentlength:76RequestMethod:POSTRequestURL:/signupBody:POST/signupHTTP/1.1Host:127.0.0.1:8080Accept:/Accept-Enc

go - 单个 defer func() 可以被其他函数共享吗?

我看到很多在函数内部使用deferfunc()的例子。有没有办法避免在不同的地方重复它并像普通函数一样调用它?在此示例(以及许多其他示例)中,延迟函数嵌套在另一个函数中:packagemainimport("fmt""os")funcmain(){deferfunc(){iferr:=recover();err!=nil{fmt.Fprintf(os.Stderr,"Exception:%v\n",err)os.Exit(1)}}()file,err:=os.Open(os.Args[1])iferr!=nil{fmt.Println("Couldnotopenfile")}fmt.P

json - 戈朗 : how to parse json that get trait and data both?

我得到了一个dic数据{{"wordname":"wordmeaning"},{"wordname":"wordmeaning"},…}我想解析为单词的映射。我尝试使用interface{}编写代码,但我无法想象如何编写。感谢阅读。 最佳答案 如果您有办法将第一个和最后一个花括号更改为方括号,那么您可以执行以下操作:主要包import("encoding/json""fmt""log")funcmain(){varraw_list[]map[string]stringvarjsonText=[]byte(`[{"Cat":"smal

reflection - 如何获取在 GO 中的 func() 参数中传递的参数值?

我正在尝试在路由中创建中间件,想知道如何获取在func()参数中传递的参数值。例如:func(cappContainer)Get(pathstring,fnfunc(rwhttp.ResponseWriter,req*http.Request)){//HOWDOIGETTHEVALUESOFrwANDreqPASSEDINfnfunc()?c.providers[ROUTER].(Routable).Get(path,fn)}我查看了反射文档,但我不清楚,或者也许有更简单的方法?已编辑(解决方案)事实证明不需要反射(reflection),正如Adam在对这篇文章的回复中以及Jason

戈朗 : convert IPv4 and IPv6 addresses from text to binary form

希望发送4个字节的ipv4地址和16个字节的ipv6地址-类似于inet_pton()去吗?structsockaddr_insa;charstr[INET_ADDRSTRLEN];inet_pton(AF_INET,"192.0.2.33",&(sa.sin_addr));structsockaddr_in6sa;charstr[INET6_ADDRSTRLEN];inet_pton(AF_INET6,"2001:db8:8714:3a90::12",&(sa.sin6_addr));我知道https://play.golang.org/p/jn8t7zJzT5v-虽然IPV6地址看

戈朗 : declaring maps and slices with iota values

我有这个Go代码:packagemainimport"fmt"typebaseGroupintconst(fooGroupbaseGroup=iota+1barGroup)vargroups=[...]string{fooGroup:"foo",barGroup:"bar",}varxGroups=map[baseGroup]string{fooGroup:"foo",barGroup:"bar",}funcmain(){fmt.Println("groups")fork,v:=rangegroups{fmt.Println(k,v)}fmt.Println("xGroups")for

google-app-engine - golang 数据存储结构 : keeping field unique and required

我想知道如何最好地保证一个字段是唯一的,如果不是,则不会保存到数据存储中。另外,它应该是必需的。我将此字段用作stringID并需要它是唯一的。我知道我可以简单地尝试通过该字段获取实体并查看它是否存在并围绕它构建逻辑。但是有没有更简单的方法,比如在您的结构中声明该字段应该是唯一的和/或必需的?就像下面的模型。typeCarstruct{Regnrstring"required""unique"}谢谢! 最佳答案 来自数据存储API:Bydefault,forstructpointers,allpropertiesarepotenti