草庐IT

string-type-overview

全部标签

string - 从字符串到结构 Golang

我创建了一个函数来将特定字符串(派生自[]字节)转换为结构。在我正在构建的程序中,进行了加密和解密,并且在这两个channel之间,数据通过JSON传输。我建议的功能有效,但是当字符串变得很长时,处理时间会很长。我想问的是,是否有人知道如何加快此功能或如何更好地管理这种情况。这是函数funcDatadecrypt(input[]byte)Data{s:=string(input)vals:=strings.Replace(s,"","",-1)part:=strings.Fields(vals)c:=strings.Split(s,"int=")[1]co:=strings.Split

string - 我如何转换并附加到 Golang 中的一段字符串,一个来自 go-ping 存储库的网络类型变量?

这是完整的代码,与您在gitrepo页面上找到的代码没有太大区别。packagemainimport("fmt""github.com/go-ping""time")varstats=[][]string{nil}funcpinging(domainstring,intervalint,unitstring,exitint){current_time:=time.Now().Local()current_time.Format("02-01-2000")switchunit{case"ms":interval*=1case"sec":interval*=1000case"min":in

go - 不可能类型开关案例 : ErrorType (type reflect. 类型)不能有动态类型 *os.SyscallError(缺少对齐方法)

尝试确定连接错误是什么并将值返回给程序。d:=net.Dialer{Timeout:20*time.Second}conn,errors:=d.Dial("tcp",fmt.Sprintf("%v:%v",host,port))iferrors!=nil{ifoerr,ok:=errors.(*net.OpError);ok{ErrorType:=reflect.TypeOf(oerr.Err)switchErrorType.(type){case*os.SyscallError:fmt.Println("connect:connectiontimedoutto",host,"onpo

string - 将 time.Time 转换为字符串

我正在尝试将一些值从我的数据库添加到Go中的[]string。其中一些是时间戳。我得到错误:cannotuseU.Created_date(typetime.Time)astypestringinarrayelement我可以将time.Time转换为string吗?typeUsersSessionstruct{UseridintTimestamptime.TimeCreated_datetime.Time}typeUsersstruct{NamestringEmailstringCountrystringCreated_datetime.TimeIdintHashstringIPst

go - "cannot use as type string in assignment"

我有以下字符串:-1,856,32,0,0,0.000000,0.0000000,0,0,137,0,0,0,1400,0,101,0,0,0,42,00,0,0,0,0,0,0,00,0,0,0,0,0,554,0-1,841,1,0,0,0.000000,0.0000000,0,0,163,0,0,0,1820,0,120,0,0,0,43,00,0,0,0,0,0,0,00,0,0,0,0,0,517,0然后我使用分隔符-1拆分它,这意味着将有一个由2个元素组成的数组(我们称它为array1)。现在,假设array1的第一个元素我想用\r\n再次拆分它,这将是一个数组(array

go - Reflect showing different a different type than 错误

背景:我正在使用govmomi收集vmware的配置。我目前正在获取我需要的数据存储信息。我需要的字段之一是磁盘Naa。这可以在Vmfs字段下的VmfsDatastoreInfo结构中找到。问题:我在一个范围内循环,我认为Ds.Info属于VmfsDatastoreInfo类型,所以理论上我可以通过Ds.Info.Vmfs获得我需要的信息。当我引用这个时,我得到了错误:ds.Info.Vmfsundefined(typetypes.BaseDatastoreInfohasnofieldormethodVmfs)出于好奇,我使用反射进行了探索并执行了以下操作:fmt.Println(re

go - Ref "is not a type"- 在结构中存储类型

我有这样一个文件:packagefootypeHandlerstruct{}然后在另一个文件中,我有:import("handlers/foo""handlers/bar""handlers/baz")typeAllHandlersstruct{Foofoo.HandlerBarbar.HandlerBazbaz.Handler}然后在另一个文件中我有:all:=routes.AllHandlers{}foo:=all.Foo{}bar:=all.Bar{}baz:=all.Baz{}但它给了我这个错误:Fooisnotatype我可能犯了一些严重错误。我想要做的是将所有处理程序存储在

elasticsearch - {"error":"Content-Type header [] is not supported","status":406} When Inserting Data to Elasticsearch with Golang

有谁知道如何解决这个错误?我用Golang向elasticsearch中插入数据,但是好像因为这个错误没有插入数据。{"error":"Content-Typeheader[]isnotsupported","status":406}我已经设置了内容类型。注意我用的是elasticsearch6.4.3request,err:=http.NewRequest("POST",urlSearch,bytes.NewBuffer(query))request.Close=truerequest.Header.Set("Content-Type","application/json")最后但同

elasticsearch - 如何在 Elastigo 中设置 Content-Type header

我在尝试使用elastigo将数据插入elasticsearch时收到此错误。错误是{"error":"Content-Typeheader[]isnotsupported","status":406}有谁知道如何将内容类型header设置为elastigo?我认为Elastigo不适用于elasticsearch6.4.3,我是否应该更改为olivere包来向elasticsearch发出请求? 最佳答案 shouldIchangetooliverepackagetomakerequeststoelasticsearch?是的

string - 一行将golang中的[]int转为[]string

我是Go的初学者。将一片整数转换为一片字符串似乎很简单,只需几行:nums:=[]int{1,2,3,4}sNums:=make([]string,len(nums))fori,x:=rangenums{sNums[i]=strconv.Itoa(x)}但是,我想知道是否有一种使用内置/标准库的快速单行代码来完成此操作的方法,类似于Python中的以下内容:sNums=map(str,nums) 最佳答案 看来您正在寻找一个对slice进行操作的map函数。这需要泛型,从Go1.11开始,泛型还不受支持。标准库也不为这些类型的操作(