Questions tagged [reflect]

123 questions
-1
votes
1 answer

Explain the printed values of method expressions

The following code tries to show the address of a method associated to struct. package main import ( "fmt" "reflect" ) type II interface { Callme() } type Str struct { I int S string } func (s *Str) Callme () { …
river
  • 694
  • 6
  • 22
-1
votes
1 answer

IOS swift get a class by a string

I create two view. 1. ViewController by storyboard 2. A UIView by new a class. I want to use UIView class name: "ViewOne" to convert to a class, then load this UIView into the ViewController. viewcontroller code: override func viewDidLoad() { …
A.Sheh
  • 61
  • 8
-2
votes
3 answers

how to use reflect know the slice data type?

i want to fill the slice data , but using reflect.kind() only let me know field(0) is slice , but i don't know it is which type of slice , it culd be int[] or string[] or other type slice befor i know what slice data type , hastily set value will…
AM031447
  • 475
  • 1
  • 8
  • 21
-2
votes
1 answer

When type is []byte, it fails at case statement in golang

func Test(val any) { switch reflect.TypeOf(val) { case nil://ok case string://ok case []byte://error here,expected expression } } As the code above, I am trying to make some statement according to the type of input value.…
tanxin
  • 145
  • 10
-2
votes
1 answer

Extract value from an array in a Json

I'm trying to get "boots" value My func main varjson:=`{ "identifier": "1", "name": "dumbname", "person": { "items": null, "inventory": [ { "T-shirt": "black", "Backpack": { "BigPocket": { …
NexDanger
  • 43
  • 11
-2
votes
1 answer

Set interface values returned by interface method with reflect

Given an interface that returns a slice of pointer types but wrapped as an interface type. Is it possible to modify the values? type Base interface { Do() } type Meta interface { Base Children() []Base } type A struct { } func (a *A)…
Mahoni
  • 7,088
  • 17
  • 58
  • 115
-2
votes
1 answer

Is it possible to add a key-value pair to a map of unknown type in Go?

I have multiple JSON files with different formats. All of them consist of an array which contain maps. The maps, however, have different structures. a.json [ { "a": "b", "c": ["d", "e"] } ] b.json [ { "f": ["g",…
-2
votes
1 answer

Cast an interface using a pointer of a Slice to iterate its values

Context: Using a custom Binder and Validator in Echo Framework. The binder use a signature of (interface{}, echo.Context), but a pointer is always passed and checked by echo.DefaultBinder. I'm having trouble validating an array of struct for some…
Alex Boutin
  • 157
  • 1
  • 8
-2
votes
1 answer

Loop through the Nth fields one time of struct n a Go

Iterate through the fields of a struct in Go I have read above thread and now I'm trying to extend it by processing multiple items at a time package main import ( "fmt" "reflect" ) type BaseStats struct { value1 int value2 …
GarfieldCat
  • 389
  • 4
  • 12
-2
votes
2 answers

Print the key/value types of a Golang map

I am trying to print the type of a map, eg: map[int]string func handleMap(m reflect.Value) string { keys := m.MapKeys() n := len(keys) keyType := reflect.ValueOf(keys).Type().Elem().String() valType := m.Type().Elem().String() …
user12849275
-2
votes
1 answer

Is there a way to handle generic function in go

I am new to go, and wanted to implement a scheduler that will recieve tasks(i.e interval + handler). I got stuck in the part where I should get the handlers as go is a static language(I can't get a generic function signature). I came up with the…
-2
votes
1 answer

`reflect.Kind()` of Slice Becomes `Struct` When Stored Inside Pointer

When checking the reflect.Kind() of a slice, reflect correctly identifies it as a slice when not stored inside a pointer: package main import ( "fmt" "reflect" ) type Dog struct { Name string Age int } func main() { var dogs…
Floating Sunfish
  • 4,920
  • 5
  • 29
  • 48
-2
votes
1 answer

How to specify the field we wanna use from a struct?

I have a struct composed of multiple fields of same type. type test struct{ A int B int C int } I want to apply a function that does the same things to the three fields, but I only wanna do it on one each time. function…
Dylan
  • 197
  • 1
  • 2
  • 10
-3
votes
1 answer

In this case why do i need to convert the value type to interface{} when the receiving parameter of the function is already interface?

table := s.Model(reflect.New(destType).Elem().Interface()).RefTable() func (s *Session) Model(dest interface{}) *Session { log.Info(reflect.TypeOf(dest)) if s.refTable == nil || s.refTable != dest { s.refTable = schema.Parse(dest,…
Pablo
  • 5
  • 1
-3
votes
3 answers

Why is "int" convertible to "string"?

This example shows that int type is convertible to string type. But my question is why? package main import ( "fmt" "reflect" ) func main() { it := reflect.TypeOf(42) st := reflect.TypeOf("hello") fmt.Printf("%q is convertible…
riad
  • 23
  • 1
  • 5
1 2 3
8
9