How to programmatically get the number of fields of a struct?

Are there any possible API like field_count() or is it only possible to get that via macros? There is no such built-in API that would allow you to get this information at runtime. Rust does not have runtime reflection (see this question for more information). But it is indeed possible via proc-macros! Note: proc-macros are … Read more

How do I find the size of a struct? [closed]

#include <stdio.h> typedef struct { char* c; char b; } a; int main() { printf(“sizeof(a) == %d”, sizeof(a)); } I get “sizeof(a) == 8”, on a 32-bit machine. The total size of the structure will depend on the packing: In my case, the default packing is 4, so ‘c’ takes 4 bytes, ‘b’ takes one … Read more

Difference using pointer in struct fields

Right, there’s a number of things to consider. First up: let’s start with the obvious syntax error in your pointer example: type Employee struct { FirstName *string `json:”name”` Salary *int `json:”salary”` FullTime *bool `json:”fullTime”` } So I’ve moved the asterisk to the type, and I’ve captialized the fields. The encoding/json package uses reflection to set … Read more

myView.frame.origin.x = value; does not work – But why?

There are two distinct dot syntaxes being used here. They look the same, but they do different things depending on what they are operating on and what is being done with it: The first myView.frame is a shorthand for [myView frame], a method call that returns a CGRect struct by value. myFrame.origin.x is accessing ordinary … Read more