Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement of data type assertion compatibility #44657

Closed
focusonline opened this issue Feb 27, 2021 · 2 comments
Closed

enhancement of data type assertion compatibility #44657

focusonline opened this issue Feb 27, 2021 · 2 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@focusonline
Copy link

If is it possible to get assertion more simple and compatible?
because larger data type can cover any type smaller than it.
for example, int64 can cover all integer type and uint64 can cover all unsigned integer type and float64 can cover float32,
so the below code is reasonable

   var i inteface{}
   i = int8(0) // assign uint 0  to i
   j := i.(int64) // anyway variable i is an integer, but actually,"interface {} is int8, not int64" will be thrown

since generic type is taken into account, the above enchancement is worth considering.
thanks for any improvement of golang, it is awesome.

@ianlancetaylor
Copy link
Contributor

I recommend using reflect.ValueOf(i).Int().

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Feb 27, 2021
@focusonline
Copy link
Author

I recommend using reflect.ValueOf(i).Int().

Thanks for reply, it is a good solution, but I still wonder, if reflect has worse performance than assertion? actually, we do not need to squash the code for extreme performance, just being curious.
Thank you! It resolved my problem already. the type is in a scope, and converted into the maximal type of it.

@golang golang locked and limited conversation to collaborators Mar 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants