jjzjj

type-conversion

全部标签

c - 直接C指针转换

我有这个C代码:uint8_t*data[BUF_SIZE];data=...;//externvoidgoReadData(uint8_t*data,intbufferSize);goReadData(data,BUF_SIZE)在GO代码中,我试图将data指针用作GO数组或slice,我想从*C.uint8_t中检索一个[]uint8。我知道data的大小//exportgoReadDatafuncgoReadData(data*C.uint8_t,bufferSizeC.int){fmt.Printf("Datatype%v\n",reflect.TypeOf(data))//

json - 将 JSON 文件读入自定义类型结构,我需要在需要类型字符串而不是自定义类型的函数中使用键

我正在创建一个工具,它可以获取JSON文件,然后使用Go从中创建PDF这是我的JSON示例:[{"Name":"Ollie","Age":"25","Comment":"Thisismycomment"},{"Name":"Amy","Age":"28","Comment":"Anothercomment"},{"Name":"Joey","Age":"19","Comment":"CommentfromJoey"},{"Name":"James","Age":"23","Comment":"James'comment"},{"Name":"Richard","Age":"20","C

go - `type foo struct` 和 `type foo []struct` 之间的区别

这些结构之间的主要区别是什么?typefoostruct{Namestring`json:"name"`}和typefoo[]struct{Namestring`json:"name"`} 最佳答案 typefoo1struct{Namestring`json:"name"`}typefoo2[]struct{Namestring`json:"name"`}简单理解为typefoo2[]foo1 关于go-`typefoostruct`和`typefoo[]struct`之间的区别,我们

unit-testing - 如何修复测试用例中的 "missing type in composite literal"

我正在尝试为函数ReadField()编写测试代码,但我在定义测试用例时遇到了困难。它给出了一个错误“复合文字中缺少类型”。我相信这只是一些语法错误。我已经尝试在函数体之外定义结构体,但它仍然会给出相同的错误。ReadField(string,string,bool)(bool,string)funcTestReadField(t*testing.T){testCases:=[]struct{NamestringInputstruct{FirstStringstringSecondStringstringSomeBoolbool}Expectedstruct{IsValidboolMe

go - "Missing type in composite literal"结构中映射的匿名列表

编辑:编译错误在Missingtypeincompositeliteral与我的问题相同,它们的组成差异很大,以至于我不明白我将如何将解决方案应用到我的程序中,因此创建了这个问题。我是新来的,我正在尝试为我已验证可以成功调用的函数编写测试,如下所示:funcmain(){items:=[]map[string]int{map[string]int{"value":100,"weight":5,},map[string]int{"value":90,"weight":2,},map[string]int{"value":80,"weight":2,},}fmt.Println(KnapS

go - Go 中的 128 位 float - 相当于 C#'s "decimal"Type

简单的问题:是否有C#的decimal的等价物?输入Go标准库?我打算使用此类型与在某些端点返回小数的ODataAPI接口(interface),据我阅读,complex128不适合此行为(它的存储不同于花车) 最佳答案 标准库的math/big包括一个Floattype:AnonzerofiniteFloatrepresentsamulti-precisionfloatingpointnumber 关于go-Go中的128位float-相当于C#'s"decimal"Type,我们在S

Golang 类型在指向一种类型 slice 的指针 slice 与另一种类型 slice 之间的类型转换

我是Golang的新手。当我尝试它时,出现编译错误:cannotusea.B(type[]*C)astype[]Zinfieldvalue代码:packagemaintypeAstruct{B[]*C}typeCstruct{charstring}typeXstruct{Y[]Z}typeZstruct{charstring}funcdoSomething(rinterface{})X{a:=r.(*A)returnX{Y:a.B,//cannotusea.B(type[]*C)astype[]Zinfieldvalue}}funcmain(){something:=&C{"abc"}

arrays - 不能在赋值 : need type assertion 中使用字(类型接口(interface) {})作为类型字符串

我是Go的新手,出于某种原因我正在做的事情对我来说似乎不是很直接。这是我的代码:for_,column:=rangeresp.Values{for_,word:=rangecolumn{s:=make([]string,1)s[0]=wordfmt.Print(s,"\n")}}我得到了错误:不能在赋值中使用word(typeinterface{})作为类型字符串:需要类型断言resp.Values是一个数组数组,所有数组都填充有字符串。reflect.TypeOf(resp.Values)返回[][]interface{},reflect.TypeOf(resp.Values[0])

戈朗 : How to declare returned variables WITH TYPE?

在GO中,如何声明WITHTYPE函数的返回变量?例如我有这个代码dat,err:=ioutil.ReadFile("/tmp/dat")check(err)fmt.Print(string(dat))但我想要的是:vardat[]byte,errerror:=ioutil.ReadFile("/tmp/dat")check(err)fmt.Print(string(dat))然而,无论我如何尝试,我都只能得到这个输出syntaxerror:unexpectedcomma,expectingsemicolonornewlineor}我在没有IDE的情况下工作,随着变量数量的增加,必须将

string - 为什么string不能转为uint8和int32以外的其他数据类型数组?

当我尝试将string转换为[]int时,编译失败。我发现字符串可以将转换为int32(rune)和uint8(byte)。这是我的测试代码:s1:="abcd"b1:=[]byte(s1)r1:=[]rune(s1)i1:=[]int8(s1)//error 最佳答案 简短的回答是因为语言规范不允许。非常量值的允许转换:Spec:Conversions:Anon-constantvaluexcanbeconvertedtotypeTinanyofthesecases:xisassignabletoT.ignoringstructt