go-interface

    4热度

    1回答

    我有一片interface{},我需要检查这个片是否包含指针字段值。 澄清例如: var str *string s := "foo" str = &s var parms = []interface{}{"a",1233,"b",str} index := getPointerIndex(parms) fmt.Println(index) // should print 3

    0热度

    1回答

    我想知道如何在使用interface{}值时使用反射设置变量,并且可以将所有类型的结构体传递给func F(o interface{})。如何将第一个值(s.A)更改为'hello'? package main import ( "fmt" "reflect" ) type T struct { A string } func main() {

    1热度

    1回答

    给定一个接口,我该如何获取指向底层值的指针? 我天真的尝试是使用类型声明是这样的: var mytypeptr *MyType = myinterface.(*MyType) ,但我得到: interface conversion: MyInterface is MyType, not *MyType

    5热度

    1回答

    我最近开始学习Go并面临下一个问题。我想实现Comparable接口。我有下面的代码: type Comparable interface { compare(Comparable) int } type T struct { value int } func (item T) compare(other T) int { if item.value < ot

    2热度

    2回答

    我一直在读the go-lang interface doc;但它仍然不清楚,如果有可能实现我想要的 type A struct { ID SomeSpecialType } type B struct { ID SomeSpecialType } func (a A) IDHexString() string { return a.ID.Hex() }

    0热度

    1回答

    获得属性我想要得到的v.val,但去编译扔我一个错误: v.val undefined (type testInterface has no field or method val) 但在v.testMe方法,它的工作。 package main import ( "fmt" ) type testInterface interface { testMe() }

    -1热度

    1回答

    我想创建一个抽象函数,它从DB获取数据并通过此数据填充数组。数组的类型可以不同。由于性能问题,我想不做任何反应。 我只想调用GetDBItems()这样的函数,并从数据库中获取所需类型的数据。但是我创建的所有实现都是有缺陷的。 下面是这个功能的实现: type AbstractArrayGetter func(size int) []interface{} func GetItems(arra

    3热度

    3回答

    我是新手gopher,试图让我的头在指针接收器和接口周围。 type Foo interface { foo() } type Bar struct {} func (b *Bar) foo() {} 根据上述定义.. --- Allowed --------- b := Bar{} b.foo() --- Not allowed ----- var foo Foo

    1热度

    1回答

    我有一个参数类型为interface{}的函数。这个参数代表我的模板数据。所以在每个页面上它存储不同的数据类型(主要是结构)。我想附加一些数据到这个参数的数据,但它是一个interface{}类型,我不能这样做。 这是我的尝试: func LoadTemplate(templateData interface) { appendCustomData(&templateData)

    0热度

    1回答

    最近,我偶然发现了一些让我在golang中循环的东西。 package main import ( "net/http" "bytes" "fmt" "io/ioutil" ) func createRequest(method, uri string, data *bytes.Buffer) string { req, err := ht