草庐IT

int-vs-trunc

全部标签

go - 运行 go 应用程序时出现 VS Code 错误

我是新手,正在学习在线教程。我从VSCode得到这个错误“不能在router.Get的参数中使用c.ReadConfig(类型func(http.ResponseWriter,*http.Request))作为类型http.Handler:func(http.ResponseWriter,*http.Request)没有实现http.Handler(缺少ServeHTTP方法)”。我检查了Get和Redconfig函数,它们看起来没问题。他这边的老师没有收到错误,他能够很好地运行Go代码。这是主要的片段这是主要功能funcmain(){config:=domain.Config{}co

PHP vs Golang http 调用得到不同的结果

我正在尝试在GoogleAppEngineGo中实现以下PHP代码:".print_r($dec,true)."";return$dec;}api_query();执行时,代码返回一个JSON值数组。我尝试在Golang中实现相同的代码:funcPrivateCall(cappengine.Context)(map[string]interface{},error){AuthAPI:="https://api.cryptsy.com/api"APIKey:="90294318da0162b082c3d27126be80c3873955f9"tr:=urlfetch.Transport{

sql - 将 int 转换为 int32

我正在努力从Db中提取行并将结果存储在slice/数组中我需要计算总记录数和总页数。pseudoCode::perPageint32:=(somenumber)totalRecords:=len(array)totalPages:=perPage/totalRecords我一直收到这个错误。terminal::`invalidoperation:perPage/totalRecords(mismatchedtypesint32andint)` 最佳答案 len(array)返回int将其转换为int32int32(len(array

Go big int 文字溢出 int64

有没有办法在Go中将一个巨大的整数文字分配给一个大Int?//error:constantoverflowsint64vara=big.NewInt(20988936657440586486151264256610222593863921) 最佳答案 啊,呃。a.SetString("20988936657440586486151264256610222593863921",10) 关于Gobigint文字溢出int64,我们在StackOverflow上找到一个类似的问题:

go - 压缩存储在 big.Int 中的余额的最佳方法是什么?

我需要将以太坊(加密货币)余额导出到Postgres,但我需要将它们压缩成一个blob,因为它们太多了,我必须为每个block存储状态。余额存储在big.Int中,但大多数帐户的余额为0(或非常接近于0),所以我想到了这种压缩算法:Format(singlerecord):8bits:thelengthofthebitstringfollowingbits:theactualbig.IntconvertedintobitswithInt.Bits()function余额以1/10^18的精度存储,因此1个以太币存储为1位和18个零。我的算法是最好的压缩方法吗?或者有更好的主意吗?例如,

json - Golang 将 json 值从 int 转换为 string

我收到json响应。其中有键pk,它的值为int。我需要将其转换为字符串,最简单的方法是什么?这是例子“pk”:145250410我需要它“pk”:“145250410”我无法制作模型并解析它,因为我并不总是知道我的json会是什么样,但我知道它总是有pk,所以这就是我解析它的方式。varbdocinterface{}bson.UnmarshalJSON([]byte(gjson.Get(*str,"user").String()),&bdoc)唯一的问题是我得到的pk是int而不是string。 最佳答案 packagemaini

postgresql - 如何将 pgtype.Int4Array(来自 pgx 库)转换为 []int64 Golang 类型?

我使用Go和Postgres(使用pgxdriver)在我的Postgres表中,我有一个包含整数数组的字段。我创建了一个变量来存储扫描后的整数数组。varidspgtype.Int4Array如何将ids转换为[]int64? 最佳答案 使用ids.AssignTo(&sliceOfInt64) 关于postgresql-如何将pgtype.Int4Array(来自pgx库)转换为[]int64Golang类型?,我们在StackOverflow上找到一个类似的问题:

VS 代码中的 Go linter 不适用于跨多个文件的包?

我已经在MacOS上的VisualStudioCode中安装了Go扩展(版本0.11.4):但是,我发现linter不会“拾取”定义在同一个包中的函数,而是在不同的文件中。例如,如果我在同一目录中创建一个文件foo.gowithpackagefoobarimport"fmt"funcmain(){fmt.Println(SayHello())}和一个文件bar.gowithpackagefoobarfuncSayHello()string{return"Hello,world!"}然后在foo.go中我得到一个linter错误,指出SayHello是一个undeclaredname:我

arrays - 如何在 Go 中定义包含 int 的 slice 和字符串的 slice ?

它看起来像这样:[[1,["a","b","c"]],[2,["z","x","y"]]]直觉上我会做类似[][]int[]string的事情,但那是无效的:syntaxerror:unexpected[,expectingsemicolonornewlineor},那么我该怎么做呢? 最佳答案 T的slice:varx[]TT的slice的slice:varx[][]TT1和T2的slice:您需要将T1和T2放入一个结构中。Sofor:sliceof(slicescontaining{intandasliceofstrings}

reflection - 将 reflect.Value 从字符串转换为 int

我遇到这样的错误reflect.Value.Convert:valueoftypestringcannotbeconvertedtotypeintgoroutine6当我运行这段代码时param:="1"//typestringps:=fn.In(i)//typeintif!reflect.TypeOf(param).ConvertibleTo(ps){fmt.Print("Couldnotconvertparameter.\n")//thisisprinted}convertedParam:=reflect.ValueOf(param).Convert(ps)我能否以某种方式做到这一