I want to make use of something like func abc[T any](collections []T)
in my Go code but it throws error:
type parameters require go1.18 or later
I don't want to upgrade it to 1.18 or later. Is there any way?
CodePudding user response:
Technically yes, you could get some generic code to compile — with severe limitations — on Go 1.17, using dev flags; however there is no good reason to do that, whatsoever.
The only sensible and straightforward thing to do is to upgrade to Go 1.18.
CodePudding user response:
@mohit-singla you can try to find a generics preprocessor. For example, here is a very dated example of one (I have not used myself). Which version of Go are you using? Can you provide details on why you are holding off 1.18?
https://github.com/diffeo/gogen
As others have stated, I would warn you about using something like this instead of upgrading to 1.18. Once the project has other people involved, few people will know how to manage it.
Another alternative is for you use any
along with type assertion/reflection. While you can do "generic" things, it makes for less manageable code, again look to 1.18.