veandco / go-sdl2

SDL2 binding for Go
https://godoc.org/github.com/veandco/go-sdl2
BSD 3-Clause "New" or "Revised" License
2.17k stars 219 forks source link

Avoid assuming that Go structs match C strucs #501

Open Keithcat1 opened 2 years ago

Keithcat1 commented 2 years ago

I noticed that Go-SDL2 seems to deal with structs by defining Go structs that have the same field layout as SDL2 structs. However, this is unsafe because if SDL2 updates and silently adds or changes struct fields, the C compiler will figure it out and throw errors if your code is now invalid, but Go could randomly cause undefined behavior (this happened for me in github.com/gen2brain/malgo and there is a PR from me to fix it in there somewhere). This can be avoided by declaring Go structs that map more or less to the SDL2 structs and manually convert between C and Go representations. This also gives us the benefits of being able to use native Go types like string instead of char*, bool instead of int and func() instead of C function pointers which are harder to use anyway.

veeableful commented 2 years ago

Hi @Keithcat1, thanks for the suggestion! Would you be submitting a PR that implements this change? Otherwise, I will have a look at it when I have more time in my hand.