草庐IT

int_types

全部标签

golang simplejson mustint64 不会从字符串转换为 int64

我正在使用simplejson,它提供了类型断言器。fmt.Printf("%s%s",m.Get("created_time").MustString(),m.Get("created_time").MustInt64())上面的代码显示了这个结果:1506259900%!s(int64=0)所以MustInt64()给出0而不是转换后的Int64值。是不是因为1506259900太大了无法转换?感谢您的帮助! 最佳答案 原始的json是:{"created_time":"1505733738"}不是{"created_time"

mysql - golang mysql DESCRIBE表导致driver.Value type nil错误

我第一次尝试使用golang查询MySQL数据库,但是当我运行命令gorunmain.go时出现以下错误。2017/10/2221:06:58sql:Scanerroroncolumnindex4:unsupportedScan,storingdriver.Valuetypeintotype*stringexitstatus1这是我的main.go主.gopackagemainimport("log""database/sql")import_"github.com/go-sql-driver/mysql"vardb*sql.DBvarerrerror//mainfunctiontob

go - JSON 响应为 int,但为空时为字符串

我正在将JSON响应解码为一个结构。对于其中一个字段,它返回一个int和一个字符串(如果为空)。typeexamplestruct{Positionint`json:"position"`}json:cannotunmarshalstringintoGostructfield.positionoftypeint响应是{"position":8}or{"position":"none"}如何处理int和string响应? 最佳答案 将类型更改为interface{},然后您可以在运行时检查类型。typeexamplestruct{Po

go - 为什么 int64(1.1*float64(time.Minute)) 是错误而 int64(0.5*float64(time.Minute)) 不在 golang 1.9.2 中?

我在golang1.9.2中遇到了一个非常奇怪的错误:当我尝试编写int64(1.1*float64(time.Minute))时显示错误。编译器说常量被截断为整数。但是当我将1.1更改为其他float(如1.20.51.7)时,它会编译!而且当我这样写的时候它也可以编译:value:=1.1*float64(time.Minute)fmt.Println(int64(value))这是go本身的一些错误吗?我在ubuntu14.04x64上运行go 最佳答案 常量1.1*float64(time.Minute)有小数部分(值大约为

sorting - go type conversion - 使用共享接口(interface)对 2 片不同的接口(interface)进行排序

下面的示例包含2个接口(interface)Foo和Bar,它们都实现了相同的接口(interface)Timestamper。它还包含实现sort.Interface的类型ByTimestamp.如函数main所示,我想使用类型ByTimestamp对Foo的slice和slice进行排序条形图。但是,代码将无法编译,因为它无法将foos(类型[]Foo)转换为ByTimestamp类型,并且无法将bars(类型[]Bar)转换为ByTimestamp类型。是否可以使用实现sort.Interface的单一类型对实现相同接口(interface)的2个不同接口(interface)s

google-cloud-platform - `datastore: invalid entity type` 来自 `datastore.NewQuery("的错误 ").Ancestor(myKey)`

我在我的Go应用程序中使用"cloud.google.com/go/datastore"库(下面的库版本),遇到错误datastore:invalidentity当我使用Ancestor()查询时键入。这是我的方法调用:ctx:=context.Background()client,err:=datastore.NewClient(ctx,"MyProjectId",option.WithCredentialsFile(myJsonFile))//...errcheck...myId:=112233myKey:=datastore.IDKey("MyKind",myId,nil)que

go - MapScan 无法解码为非指针 int64

我使用了MapScan并用这个错误对其进行了迭代cannotunmarshalintonon-pointerint64第一次迭代后出错。这是我正在处理的代码:typeNotFinishedTBLFieldsstruct{Bulk_idint64RecipientstringOperatorstringTracking_codestring}funcFetchNotFinishedTBLRows()*NotFinishedTBLFields{rowValues:=make(map[string]interface{})varrowNotFinishedTBLFieldsiter:=Ins

postgresql - Golang 和 Postgres 的交互——每次查询都从 int64 表中减去 1

我正在学习创建HTTPAPI,我正在创建一个虚拟的API信用系统,因为这是我特别感兴趣的。这是我想出的:(我正在使用PQDriver)if!dummy.creds但是,假设我将SQL表编辑为50个积分,每次查询虚拟API时如何减去1个积分?例如,如果我查询API并且它从SQL表返回成功的虚拟数据(JSON格式),我如何减去一个学分,这样我只有49个学分,然后是48、47、46,等等更新:这是我想出的_,err:=db.Exec("UPDATEdummySETcreds=-1WHEREemail=$1")iferr!=nil{log.Fatal(err)}它没有工作,没有做任何事情。我做

mongodb - 如何使用 mgo 将 int slice 传递给 "$in"

我在使用mgo的bson功能创建查询时遇到了一些麻烦。我只是想做{'search_id':{'$in':[1,2,4,7,9]}},但我不知道该怎么做在mgo中。我有一片int,并尝试直接传递它:toRemove:=[]int{1,2,4,7,9}err=coll.Remove(bson.M{"search_id":bson.M{"$in":toRemove}})我看到另一篇帖子建议我需要使用[]interface{},但这也不起作用:toRemoveI:=make([]interface{},len(toRemove))foridx,val:=rangetoRemove{toRemo

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