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