Is it common for members to be pointers in the Golang?

Take, for example, go github package . Almost every member of each particular structure is a pointer to a value, not a value.

Is this an idiomatic go? Why?

I understand that it reduces the size of the structure (provided that the size of the pointer is smaller than the size of the value that it points to), which can be important if you walk around the structures a lot by value. But why not create a value structure and pass a structure instead of a pointer?

+5
source share
1 answer

In the github package, the reason is that most structures are designed to serialize / deserialize from json from the github api.

The reason they use *int instead of int is because the null pointer value is nil and the null int value is 0 . This allows the client to distinguish between "this field was not included in the response" and "the value of this field is zero."

This is especially useful for things like times when, if you don't have null types, you will have many dates like 00-00-0000.

+9
source

Source: https://habr.com/ru/post/1234577/


All Articles