Structs Enums

26 articles
Anonymous structs Anonymous structs are unnamed struct literals defined inline to create temporary data structures without declaring a new type. Compare structs Go structs are compared by value using the `==` operator, but only if all their fields are comparable types. Convert between structs Go requires manual field mapping or reflection to convert between structs like tar.Header and zip.FileHeader. How to Compare Structs in Go Use the == operator for simple structs or reflect.DeepEqual for structs containing slices, maps, or functions. How to Convert a Struct to JSON in Go Convert a Go struct to JSON using the encoding/json.Marshal function. How to Convert JSON to a Struct in Go Decode JSON data into a Go struct using the encoding/json.Unmarshal function with a pointer to the target struct. How to Copy a Struct in Go (Shallow vs Deep Copy) Go copies structs by value (shallow), requiring manual recursion or Clone() methods for deep copies of nested pointers. How to Create an Immutable Struct in Go Go structs are mutable by default; enforce immutability by using unexported fields and omitting setter methods. How to Create a Struct Constructor Function in Go Create a Go struct constructor by defining a function that returns a pointer to an initialized struct instance. How to Create a Type-Safe Enum with Structs in Go Create a custom type and constants in Go to simulate type-safe enums and enforce valid values at compile time. How to Create Enums in Go (iota Patterns) Use the iota keyword inside a const block to automatically generate sequential integer values for Go enum constants. How to Define and Use Structs in Go Define Go structs with the type keyword and field list, then instantiate them using struct literals with field names. How to define struct Define a Go struct using the type keyword followed by the struct name and a block of named fields with their types. How to Implement the Stringer Interface for Custom Types Implement the String() method on your type to satisfy the fmt.Stringer interface for custom formatting. How to Initialize a Struct in Go (Literal, New, and Zero Value) Initialize Go structs using literals for immediate data, new() for zero-value pointers, or variable declaration for zero values. How to Marshal and Unmarshal Nested Structs in Go Convert nested Go structs to JSON bytes using json.Marshal and parse them back using json.Unmarshal. How to Use Anonymous Structs in Go Anonymous structs in Go are inline-defined data structures without a type name, used for temporary or one-off field groupings. How to Use Struct Field Visibility (Exported vs Unexported) Go struct fields starting with a capital letter are exported for external access, while lowercase fields remain private to the package. How to Use Struct Tags in Go (json, db, yaml, validate) Add backtick-quoted key-value pairs to Go struct fields to control JSON, database, and validation behavior. Initialize structs Initialize C-wrapped Go structs by calling the C init function via a helper method to avoid crashes from zero values. Pointer vs value receivers Use pointer receivers to modify data or save memory on large structs; use value receivers for small, read-only operations. Struct embedding Struct embedding in Go promotes fields and methods from an anonymous inner struct to the outer struct for code reuse and composition. Struct tags Struct tags are metadata strings attached to Go struct fields to control how external tools process the data. What Is iota in Go and How to Use It for Enums iota is a predeclared constant that auto-increments in const blocks to easily define sequential integer values for enums. What Is Struct Embedding in Go (Composition Over Inheritance) Struct embedding in Go enables composition by including one struct inside another, promoting its fields and methods for reuse without inheritance. Why You Shouldn't Compare Structs with == If They Contain Slices Use reflect.DeepEqual instead of == to compare structs with slices because == only checks memory addresses.