草庐IT

string-type-overview

全部标签

go - 来自博客 golang arrays slices and strings

typepath[]bytefunc(ppath)ToUpper(){fori,b:=rangep{if'a'[练习:将ToUpper方法转换为使用指针接收器并查看其行为是否发生变化。]如何使用指针方法?我试图取消引用*p并试图从范围中删除i但它一直说不匹配的类型。 最佳答案 因为path是在[]byte上定义的类型,它恰好是一个slice,所以不需要使用指针接收器,因为slice类型已经被引用类型。但是,如果需要指针接收器,则需要在方法中的所有位置取消引用指针值以获取底层slice值:func(p*path)ToUpper(){f

pointers - 转到错误 : "embedded type cannot be a pointer to interface"

以下代码给出了编译时错误:typeIFileinterface{Read()(nint,errerror)Write()(nint,errerror)}typeTestFilestruct{*IFile}错误:./test.go:18:embeddedtypecannotbeapointertointerface为什么我不能嵌入*IFile? 最佳答案 语言规范不允许。规范中的相关部分:Structtypes:Afielddeclaredwithatypebutnoexplicitfieldnameisananonymousfiel

string - 戈朗 : optimal sorting and joining strings

Thisgo源代码中的short方法有一条注释,暗示它没有以最佳方式分配内存。...coulddobetterallocation-wisehere...This是Join方法的源代码。这里到底分配了什么低效的东西?我看不到分配源字符串slice和目标字节slice的方法。源是键的slice。目的地是byteslice。 最佳答案 codereferencedbythecomment像写的那样是内存有效的。任何分配都在strings.Join中,这是为了最小化内存分配而编写的。我怀疑评论是不小心从这个codeinthenet/htt

go - undefined <type float32 has no field or method sum> 错误。如何解决?

这是我的程序。当我运行它时,它给出了以下错误-a.sumundefined(typefloat32hasnofieldormethodsum)packagemainimport("fmt")typeCalculationinterface{operation(input[]float32)}typeAdditionstruct{sumfloat32}func(aAddition)operation(input[]float32){a.sum=input[0]for_,a:=rangeinput[1:]{a.sum+=a}fmt.Println("Sum:",a.sum)}funcmai

Go AST/Types——如何判断错误?

有更好的方法吗?我需要知道v的类型是否是内置的“错误”类型。我觉得应该有一种更简洁的方法来做到这一点:import("go/ast""go/types")funcIsError(vast.Expr,infotypes.Info)bool{t:=info.Types[v]returnt.Type.String()=="error"&&t.Type.Underlying().String()=="interface{Error()string}"} 最佳答案 Typeassertion是检查变量类型的惯用方法。鉴于您正在处理一个AST表

Golang (v1.8) - 错误 : type *mux. Route has no field or method Method

我有一个配备了gorilla工具包的go/golang应用程序。我正在尝试使用gorilla/mux包进行路由。我的路线和错误信息如下。有什么指点吗?路线`r:=mux.NewRouter()r.HandleFunc("/",landing)r.HandleFunc("/contact",contact)r.HandleFunc("/faq",faq)r.HandleFunc("/register",accountsC.New).Method("GET")r.HandleFunc("/register",accountsC.Create).Method("POST")http.List

arrays - 为什么 Go 的 strings.Fields(str) 和 strings.Split(str, "") 这么慢?

我一直在测试Node和Go中的函数以比较它们的性能。几乎在每个测试中,Go都比Node快得多,除了使用strings.Fields()或strings.Split()时,Node是Node的2-3倍快。开始(2.14秒):start:=time.Now()varnewWords[]stringstr:="asdasjhfalsjdhalsdjhfadhfaldhfaljdhfaldhfasjdhfalsdhasdalsdhalksdhalksdhalksdalkjsdfadlkjdalkjdhasdhfefafad6a5a85dfas5da5dada6sd58ad5a8sd5f8as5

string - 二进制字符串转unicode

我不是100%确定为什么我的unicode二进制字符串不起作用..任何人都可以指出问题或帮助我修补它吗?另外,我将二进制文件分块的原因是它对于ParseInt来说太大而无法处理。有关示例,请参见下面的playground链接。funcbinToString(s[]byte)string{varcounterintchunk:=make([]byte,7)varbufbytes.Bufferfori:=ranges{ifi%8==0{counter=0ifi,err:=strconv.ParseInt(string(chunk),2,64);err==nil{buf.WriteStrin

string - 将字符串作为字符数组访问以在 strings.Join() 方法中使用 : GO language

我正在尝试以字符数组或rune形式访问字符串,并使用一些分隔符进行连接。什么是正确的做法。这是我尝试过的两种方法,但出现如下错误cannotuse([]rune)(t)[i](typerune)astype[]stringinargumenttostrings.Join一个字符串在GOLANG中是如何表示的。是不是有点像字符数组?packagemainimport("fmt""strings")funcmain(){vart="hello"s:=""fori,rune:=ranget{s+=strings.Join(rune,"\n")}fmt.Println(s)}packagema

string - slice 起始位置大于字符串的长度

任何人都知道为什么下面的代码运行时没有panic,它在字符串的长度上访问索引1。import("fmt")funcmain(){fmt.Println("hi"[2:])} 最佳答案 它不会“超出”长度,2恰好是长度(等于它)。Forarraysorstrings,theindicesareinrangeif0,otherwisetheyareoutofrange.由于您要对string进行slice,索引在范围内,如果:0这个表达式:"hi"[2:]由于缺少上限,它默认为长度,即2,因此它等同于:"hi"[2:2]这完全符合规范,