Validating methods in Go
When we want to make sure that a field of a struct we’ve defined is not arbitrarily set by the user of our library, but is validated against allowed values, we have some options.
One option is to make that field unexported (lowercase initial, exported-identifiers-in-go), and then creating accessor methods for the struct to set and get the field.
Another option is to make the whole struct unexported. This will make it impossible for users of our library to interact directly with the object. Then we can export a set of constructor functions and methods that will allow the user to interact with th eobject in a controlled manner, because these functions and methods will contain validation.
References
- John Arundel, For the Love of Go
#exported #programming #for_the_love_of_go #values #struct #golang #variables #methods #validation #accessor #unexported