Go Structs
A struct (short for "structure") is a collection of fields. It allows you to group different types of data into a single custom type.
Defining a Struct
Why use Structs?
- Organization: Keep related data together (e.g., a
Userstruct withID,Email, andPassword). - Method Receivers: You can attach functions (methods) to structs.
- JSON/Database Mapping: Use "tags" to tell Go how to convert a struct to JSON or a database row.
Struct Tags
Tags are small pieces of metadata added to fields, usually to help with JSON.
Important Notes
- Zero Values: If you don't provide a value for a field, it gets its "zero value" (e.g.,
0forint,""forstring). - Exporting: If a struct field starts with a Capital Letter, it is public (exported). If it starts with a lowercase letter, it is private to its package.