草庐IT

string_slice

全部标签

string - 查找符合特定要求的字符串

有一个函数应该返回true:funcaccessible(agentstring)bool{a:=strings.Split(agent,"")iflen(a)!=3{returnfalse}b:=a[0]c:=a[1]d:=a[2]x:=strings.EqualFold(b,c)y:=b!=strings.ToLower(c)z:=strings.Index(d,b+c)==1&&len(d)==5returnx&&y&&z}但是我无法确定哪个string输入符合这些要求。我错过了什么吗?PS:这是来自gocode.io的任务#3 最佳答案

reflection - Go 反射(reflect)字段索引 - 单个索引与 slice

reflect.StructField有一个类型为[]int的Index字段。关于此的文档有点令人困惑:Index[]int//indexsequenceforType.FieldByIndex当然Type.FieldByIndex也符合预期,对其行为有更清晰的解释://FieldByIndexreturnsthenestedfieldcorresponding//totheindexsequence.ItisequivalenttocallingField//successivelyforeachindexi.//Itpanicsifthetype'sKindisnotStruct.

go - 尝试将 xml.Unmarshal 构造为类型为 map[string]interface{} 的字段时出错

问题是xml.Unmarshal的字段类型为map[string]interface{}的结构将失败并出现错误:unknowntypemap[string]interface{}{XMLName:{Space:Local:myStruct}Name:testMeta:map[]}由于类型为map[string]interface{}的Meta字段是我所能定义的,因此必须动态解码其中的内容。packagemainimport("encoding/xml""fmt")funcmain(){varmyStructMyStruct//metaisasfarasweknow,insidemeta

arrays - 将非 slice 附加到 slice 图

我目前的代码是这样的:name:="John"id:="1234"c:=make(map[string][]string)c["d"]=make([]string,len(d))c["l"]=make([]string,len(l))copy(c["d"],d)copy(c["l"],l)c["test"]=namec["id"]=id假设d&l都是[]string。Go不允许我这样做。有没有一种方法可以实现这样的JSON:{“名字”:“约翰”,“编号”:“1234”,“d”:[123、456],“我”:[123、456] 最佳答案

go - 使用带有标量或 slice 的函数

我想将函数T_conv与一个float或一片float一起使用。此示例使用float作为T_conv的参数:funcT_conv(T...interface{})[]interface{}{varr[]interface{}=make([]interface{},len(T))fori,v:=rangeT{fmt.Printf("Ind:%dResult:%v,type:%T\n",i,v,v)r[i]=v.(float64)*1.00024}returnr}funcmain(){v:=T_conv(20.0)fmt.Printf("Result:%v,type:%T\n",v,v)}

pointers - 修复数组替换中引用不正确的 slice

以下go代码无法编译,因为(我相信)指针的引用方式存在错误。特别是,错误信息是prog.go:13:cannotuseappend((*x)[:remove],(*x)[remove+1:]...)(type[]int)astype*[]intinassignment这是导致此错误消息的代码的抽象和简化版本。packagemainimport"fmt"funcmain(){x:=&[]int{11,22,33,44,55,66,77,88,99}fori,addr:=range*x{ifaddr==22{forlen(*x)>5{remove:=(i+1)%len(*x)x=appen

go - 如何在方法中修改类型 slice ?

如何在方法内部修改类型slice?我试过http://play.golang.org/p/ul2n8mk6yetypeTest[]stringfunc(testTest)Add(strstring){test=append(test,str)}funcmain(){test:=Test{}test.Add("value")fmt.Println(len(test))//0}和http://play.golang.org/p/nV9IO7E5sptypeTest[]stringfunc(test*Test)Add(strstring){v:=append(*test,str)test=&

go - 在 goroutine channel 改变 slice 后,如何获取 slice 新索引?

我想知道如何在slice中获取元素新索引,我有一个函数可以从数据库中获取应用程序,并查询某些应用程序(用于过滤),但是当我查询并获得一些应用程序时,我不需要,我正在尝试将它们从slice中删除,以便只有需要的才能进入View。它正在工作,但我在goroutine中的索引是旧索引时遇到问题,所以当它尝试从slice中删除元素时它会panic:无论如何这是我的代码://ListApplicationswilllisttheapplicationsfuncListApplications(whttp.ResponseWriter,r*http.Request){session:=common

go - [Golang]为什么bufio reader改成了strings reader?

代码如下==s:=strings.NewReader("ABCDEFGJHIJK")fmt.Printf("pais%d\n",s.GetValueI())//GetValueI()returnsthevalueofr.ibr:=bufio.NewReader(s)fmt.Printf("papais%d\n",s.GetValueI())cc,_:=br.ReadByte()fmt.Printf("%c\n",cc)fmt.Printf("papapais%d\n",s.GetValueI())打印显示:帕是0爸爸是0一种爸爸12岁如此奇怪的结果..为什么bufio调用ReadByt

Golang jsonapi 需要 string 或 int 但 mongo 需要 bson.ObjectId

使用go和以下包:github.com/julienschmidt/httproutergithub.com/shwoodard/jsonapigopkg.in/mgo.v2/bson我有以下结构:typeBlogstruct{Posts[]interface{}}typeBlogPoststruct{Idbson.ObjectId`jsonapi:"primary,posts"bson:"_id,omitempty"`Authorstring`jsonapi:"attr,author"`CreatedDatetime.Time`jsonapi:"attr,created_date"`