![](/img/trans.png)
[英]How can I reach struct member in interface type
我必須在切片中保留多類型結構並為其播種。 我使用了接口類型的可變參數並 foreach 。 如果我調用接口的方法,它可以工作,但是當我嘗試訪問 struct 時,我不能。 我該如何解決? 注意: Seed() 方法返回數據的文件名。 界面: 方法: 播種機切片: 最后一個,SeedSchema fu ...
[英]How can I reach struct member in interface type
我必須在切片中保留多類型結構並為其播種。 我使用了接口類型的可變參數並 foreach 。 如果我調用接口的方法,它可以工作,但是當我嘗試訪問 struct 時,我不能。 我該如何解決? 注意: Seed() 方法返回數據的文件名。 界面: 方法: 播種機切片: 最后一個,SeedSchema fu ...
[英]Get pointers to all fields of a struct dynamically using reflection
我正在嘗試為 golang 構建一個簡單的 orm 層。 這將采用一個結構並生成cols []然后可以將其傳遞給 sql function rows.Scan(cols...)它采用結構中與它在結果集中找到的每一列相對應的字段指針這是我的示例結構 這是我的通用 ORM function 一旦此方法正 ...
[英]go reflect: how to dynamically create a pointer to a pointer to ...?
我想創建一個reflect.Value ,它表示一個指向最終值的多級嵌套指針。 嵌套級別在編譯時是未知的。 如何使用reflect創建指向指針的指針? 在嘗試創建指向指針的指針時,我已經絆倒了“不可尋址的值”障礙。dave := "It's full of stars!" stargazer := ...
[英]Append to golang slice passed as empty interface
如何附加到空接口(已驗證為 *[]struct)? func main() { var mySlice []myStruct // myStruct can be any struct (dynamic) decode(&mySlice, "...") } func decode( ...
[英]Read top level struct tags
如何從 Go 中的結構字段獲取標簽? 我有一個嵌套結構,我想將它作為參數傳遞給另一個函數並讀取那里的標簽。 我知道通過將其作為字段訪問是可能的,但我正在尋找一種方法。 ...
[英]Reading nested structure using reflection
我編寫了一個遞歸函數,它遍歷深層嵌套結構,如下所示: 如何使用反射訪問內部對象(切片、結構)以使用它們? 迭代切片我嘗試使用: ...
[英]How to get the element type of slice?
如果有這樣的結構: type A struct { Arr []int } 如何獲取切片arr的元素類型? 比如傳入一個空的A實例,如何獲取int類型? func PrintElementType(obj interface{}) { objType := reflect.Type ...
[英]How to use "reflect" to set interface value inside a struct of struct
嘗試使用“反射”package 設置接口值時遇到了麻煩。 接口值實際上是在結構的結構內。 在Go Playground中查看我的代碼基本上,在initProc中,我想將dummyAFunc function 分配給Box結構中的DummyA字段package main import ( ...
[英]Can a package be an interface in Go?
我想在 Go 中執行以下操作,其中c/c.go包含: a/a.go包含: 和b/b.go包含: 一個明顯的解決方案是開關: 但我想知道是否可以更優雅地做到這一點,也許使用reflect package。 問同樣問題的另一種方法是,package 可以成為 Go 中的接口嗎? ...
[英]In Go, how can I use reflect to set the value of a map on a struct?
在 Go 中,如何使用反射包來設置map的值? 我看到的與地圖有關的唯一Value方法是MapIndex 、 MapKeys 、 MapRange和SetMapIndex (如果map為零,則它會發生恐慌)。 我似乎無法設置 Addr,因為地圖不可尋址。 我不確定如何將上面的m分配給mappe ...
[英]Using reflect to assign a typed value
我正在研究我們的一個系統應用程序,特別是在配置文件處理位中。 我們目前有 3 個不同的地方可以存儲配置文件,以后可能會擴展。 我想做的是簡化我們需要添加新托管字段的方式。 到目前為止,我的解決方案看起來像這樣: 到目前為止,我只是缺少這件作品來完成作業。 ...
[英]Check if a method type matches a function type
給定以下示例,如何檢查方法是否匹配 function 簽名? https://play.golang.org/p/rIDfp0E14ge ...
[英]Instantiate a new obj using Go reflect and type assert to an interface
我無法解釋為什么以下內容有效。 它打印 如果它是一個 Dock 類型,那么它就不是一個指向 Dock 的指針。 為什么它符合MyInterface ? https://play.golang.org/p/Z9mR8amYOM7 評論中的 d2 示例沒有。 ...
[英]Why does reflecting the name (or package path) of the error type cause a panic in Go?
在Golang中使用反射獲取error類型的名稱或package路徑導致程序出現panic(with panic: runtime error: invalid memory address or nil pointer dereference )。 這種行為的原因是什么? (對其他內置類型執行 ...
[英]How to construct slice of structs dynamically using reflection
我試圖用指針構造 Book 結構的切片,但我無法通過 Go 中的反射使其工作。 []*Book 結構體指針的 Book 切片,請注意 scanResults 方法可能接收任何類型的切片,而不僅僅是 Book 結構體。 所以我希望在運行時動態構建一個切片你能告訴我我在下面的代碼片段中出錯了嗎? h ...
[英]How to create a new function from another function using reflection
使用此代碼作為模板package main import "fmt" type myStruct struct { Value int } type counter int func newFuncHandler(fn func(myStruct) error) (interfac ...
[英]How to compare nested structs, with same fields in child slice fields but in different order
在下面的代碼中,a1 和 a2 是相同的。 它們具有相同的字段但具有不同的順序(書籍 A 和書籍 B 的順序不同)。 當我使用 DeepEqual() 方法進行比較時,結果表明它們不相等。 如何比較它們並得出它們相等的結果? 結果: ...
[英]Iterate through a struct with an embedded struct
我有幾個正在接收數據的結構。 所有結構都應包含一些相同的數據,這些數據已嵌入到HeaderData結構中。 數據不是同時填充的,我需要一個函數來檢查是否所有字段都收到了值(不是空字符串)。 我試圖用反射來解決這個問題。 問題是,reflect 會將HeaderData視為一個字段。 這意味着它始終 ...
[英]How to check that a return value of a function satisfies the error interface
我想編寫一些代碼來檢查結構的方法並對它們進行某些斷言,例如,它們返回的最后一件事應該是error 。 我已經嘗試了以下示例腳本: 然而,這會產生一個 編譯的是最后的以下兩行: 然而,這會產生恐慌: 檢查最后一個參數是否實現了error接口的正確方法是什么? 換句話說,我如何獲得一個e ...
[英]How to create a variable of dynamic type
我能夠創建一個“樣本”類型的變量“模型”,如下所示: 我能夠成功創建它,因為我已經知道結構類型(示例)。 但是,當我嘗試按如下方式創建類似的變量 'a' 時,出現語法錯誤: 請告知如何在 Go 中創建動態類型的變量。 ...