logo
down
shadow

Why is reflect Type.Implements() so much slower than a type assertion?


Why is reflect Type.Implements() so much slower than a type assertion?

By : spopo
Date : November 20 2020, 03:01 PM
this will help Type assertion in Go relies on a function called runtime.assertE2I2. If you look into the code, you'll notice it relies on getitab which in turn relies on additab (in the same file).
Now, the actual logic of checking if the given type implements an interface inside of additab is exactly the same as Implements in reflect - a linear search, which is even pointed out in this comment:
code :
// both inter and typ have method sorted by name,
// and interface names are unique,
// so can iterate over both in lock step;
// the loop is O(ni+nt) not O(ni*nt).


Share : facebook icon twitter icon
How can I type assert a reflect.Value struct back to an interface that I know it implements?

How can I type assert a reflect.Value struct back to an interface that I know it implements?


By : LonelyCoyote
Date : March 29 2020, 07:55 AM
I hope this helps . The GetID method requires a pointer receiver, but you are operating on a struct value. Since that value isn't addressable, you can't call the GetID method, nor can you call Addr() to get a pointer to the value.
This is the same reason that this won't compile: https://play.golang.org/p/ZPQnVXtx01
code :
hid := HasID{ID: "testID"}
_ = IHasID(hid)
id, _ := GetIDFrom(reflect.ValueOf(&testy))
reflect.Set slice-of-structs value to a struct, without type assertion (because it's unknown)

reflect.Set slice-of-structs value to a struct, without type assertion (because it's unknown)


By : Ewgeny
Date : March 29 2020, 07:55 AM
wish of those help You're calling reflect.TypeOf on a reflect.Value, which is where **reflect.rtype is coming from.
Call your One function wth the like value directly, and assign that result to the slice.
code :
func One(like interface{}) interface{} {
    typ := reflect.TypeOf(like)
    one := reflect.New(typ)

    return one.Interface()
}

func Many(num int, like interface{}) interface{} {
    typ := reflect.TypeOf(like)
    many := reflect.MakeSlice(reflect.SliceOf(typ), num, num)

    for i := 0; i < num; i++ {
        one := One(like)
        many.Index(i).Set(reflect.ValueOf(one).Elem())
    }

    return many.Interface()
}
Detailed differences between type annotation `variable: type` and type assertion `expression as type` in TypeScript

Detailed differences between type annotation `variable: type` and type assertion `expression as type` in TypeScript


By : Sjoerd Dijkstra
Date : March 29 2020, 07:55 AM
it helps some times
I was wondering what is the exact difference between type annotation variable: type and type assertion expression as type
code :
const foo3 = { n: '' } as Foo; // KO: "Property 's' is missing..."
Type '{ n: string; }' cannot be converted to type 'Foo'.
  Property 's' is missing in type '{ n: string; }'.
const foo4 = { n: '' as any } as Foo; // ok
const foo5 = { n: 1, x: 2 } as Foo;   // KO: "Property 's' is missing..."
Type '{ n: number; x: number; }' cannot be converted to type 'Foo'.
   Property 's' is missing in type '{ n: number; x: number; }'.
const foo6 = { s: '', x: 2 } as Foo;  // ok
const foo7 = { s: 1, x: 2 } as Foo;   // KO: "Types of property 's' are incompatible."
Type '{ s: number; x: number; }' cannot be converted to type 'Foo'.
  Types of property 's' are incompatible.
    Type 'number' is not comparable to type 'string'.
How to use a reflect.Type to perform a type assertion

How to use a reflect.Type to perform a type assertion


By : Victor Sergeev
Date : March 29 2020, 07:55 AM
seems to work fine From the reflect.Type docs:
code :
if reflect.TypeOf(command.Request) != command.RequestType {
    log.Fatal(errors.New("invalid request type"))
}
golang type assertion using reflect.Typeof()

golang type assertion using reflect.Typeof()


By : Boris LV 呂英璋
Date : March 29 2020, 07:55 AM
To fix the issue you can do A type assertion, syntactically, takes a type in the parentheses, not an expression. So it is a syntax error.
You seem to be trying to do a type assertion with a value computed at runtime. Does that make sense? Let's think about what a type assertion is.
Related Posts Related Posts :
  • Continue loop after 404 response
  • GoLand is slow to launch a debug application
  • Golang native http client hangs on particular URI
  • End Process and Child Processes
  • How do I use exec() with input args?
  • How to understand the channel communication rules in golang memory model?
  • Aerospike 3.6.4 list insert operation giving 'Server error'
  • Implement io.Reader from image.RGBA
  • Passing variable as argument for exec.Command()
  • Using a subquery in 'FROM' in gorm
  • Gin framework can not get the data from Postman
  • golang testing command line arguments
  • Javascript's Boolean translation to Golang
  • Write log by Golang logger
  • Can I use an interface as a parameter when writing a function which could end up being passed any kind of struct?
  • how to lock function in go?
  • Create a function that test marshalling/unmarshalling from an interface
  • what is the value assigned to "bintag" after this line "binTag := field.Tag.Get("binary")"
  • What's the difference between "real" and "imaginary" parts of a complex numer in Go?
  • Difference between method signatures of structs
  • why does assigning interface with pointer and then an address shows different behaviour in Golang
  • Short circuit evaluation in Go
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org