what you may be looking at is (in the words of Rog Peppe) "structural type constraints":
https://go.dev/play/p/HiFc9CUfI7P -s On Fri May 27, 2022 at 18:32 CET, Martin Schnabel wrote: > the reason your snippet does not work is that you pass in *payload as T > and the zero value of a pointer type is nil and json unmarshal cannot > with nil pointer values. so would need to pass in payload as T and > *payload as P and somehow explain that P is *T and a > encoding.BinaryUnmarshaler. I am not versed enough in go generics to > know the solution, but would propose to take a simpler approach if > possible. > > On 5/27/22 18:24, Martin Schnabel wrote: > > how about https://go.dev/play/p/YXOTLKpvXNI using json unmarshal > > directly and without using binary marshaller. > > > > or even https://go.dev/play/p/GFE3rnyx9f8 without the endpoint abstraction. > > > > or maybe explain what you want to achieve other than unmarshalling a > > json payload? > > > > best regards > > > > On 5/27/22 17:53, Jérôme LAFORGE wrote: > >> Hello, > >> I do some tests with generics with Go 1.18. > >> But I can't reach my goal. > >> > >> With https://go.dev/play/p/7kxMEWtXcPG > >> I have: > >> ``` > >> > >> panic: json: Unmarshal(nil *main.payload) goroutine 1 [running]: > >> main.main() /tmp/sandbox3884661607/prog.go:43 +0x112 Program exited. > >> ``` > >> > >> Because payload (line 15) is nil > >> > >> Any tips ? > >> > >> Many thx. > >> > >> -- > >> You received this message because you are subscribed to the Google > >> Groups "golang-nuts" group. > >> To unsubscribe from this group and stop receiving emails from it, send > >> an email to golang-nuts+unsubscr...@googlegroups.com > >> <mailto:golang-nuts+unsubscr...@googlegroups.com>. > >> To view this discussion on the web visit > >> https://groups.google.com/d/msgid/golang-nuts/5cd2414a-ee8d-4a32-b976-d8b97d677945n%40googlegroups.com > >> > >> <https://groups.google.com/d/msgid/golang-nuts/5cd2414a-ee8d-4a32-b976-d8b97d677945n%40googlegroups.com?utm_medium=email&utm_source=footer>. > >> > >> > > > > -- > You received this message because you are subscribed to the Google > Groups "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/2aea3b7e-a34e-fe5f-be44-ba4432d46ee2%40mb0.org. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CKAPTI8K95AN.2RZ4ZZ0L5VMB7%40zoidberg.