### Install go-exhaustruct Source: https://github.com/gaijinentertainment/go-exhaustruct/blob/master/README.md Installs the exhaustruct Go analyzer using the go get command. This command fetches the latest version of the package and makes it available for use. ```shell go get -u dev.gaijin.team/go/exhaustruct/v4/cmd/exhaustruct ``` -------------------------------- ### Global Empty Struct Allowance Example (Go) Source: https://github.com/gaijinentertainment/go-exhaustruct/blob/master/README.md Illustrates the effect of the -allow-empty flag, where empty struct initializations are permitted in all contexts, including function returns, variable declarations, and slice literals. ```go package main // With -allow-empty: ALL empty structs are allowed func createConfig() Config { return Config{} // OK: empty structs allowed globally } var globalConfig = Config{} // OK: empty structs allowed globally func processConfigs() []Config { return []Config{{}} // OK: empty structs allowed globally } ``` -------------------------------- ### Return Statement Empty Struct Allowance Example (Go) Source: https://github.com/gaijinentertainment/go-exhaustruct/blob/master/README.md Provides an example of the -allow-empty-returns flag's behavior, allowing empty structs in return statements but flagging them in variable declarations or slice literals. ```go package main // With -allow-empty-returns: empty structs allowed only in return statements func createConfig() Config { return Config{} // OK: empty struct in return statement } func initializeConfig() { var config = Config{} // ERROR: empty struct in variable declaration _ = config } func processConfigs() []Config { return []Config{{}} // ERROR: empty struct in slice literal (not direct child of return statement) } ``` -------------------------------- ### Pattern-Based Allowance for Empty Structs in Go Source: https://github.com/gaijinentertainment/go-exhaustruct/blob/master/README.md Illustrates the `-allow-empty-include` flag in Go Exhaustruct, which allows empty structs for specific types matching given patterns. The example shows how to configure this for types like `Config` and `Options`, and demonstrates which initializations are permitted or denied based on the patterns. ```bash exhaustruct -allow-empty-include ".*Config.*" -allow-empty-include ".*Options.*" ./... ``` ```go package main type DatabaseConfig struct { Host string Port int } type ServerOptions struct { Timeout int MaxConns int } type UserData struct { Name string Email string } func example() { // OK: matches .*Config.* config := DatabaseConfig{} // OK: matches .*Options.* opts := ServerOptions{} // ERROR: doesn't match any pattern user := UserData{} _ = config _ = opts _ = user } ``` -------------------------------- ### Basic Struct Initialization Check (Go) Source: https://github.com/gaijinentertainment/go-exhaustruct/blob/master/README.md Demonstrates the default behavior of exhaustruct, which flags missing field initializations in structs. The example shows a Config struct and how returning an uninitialized Config struct generates an error. ```go package main type Config struct { Host string Port int Database string } // Without any flags - requires all fields func createConfig() Config { return Config{} // ERROR: missing fields Host, Port, Database } func createValidConfig() Config { return Config{ Host: "localhost", Port: 5432, Database: "mydb", } } ``` -------------------------------- ### Allow Empty Declarations in Go Source: https://github.com/gaijinentertainment/go-exhaustruct/blob/master/README.md Demonstrates the use of the `-allow-empty-declarations` flag in Go Exhaustruct. This option permits zero-value structs in direct variable declarations, useful for gradual struct population. The example shows valid and invalid uses within Go functions. ```bash exhaustruct -allow-empty-declarations ./... ``` ```go package main // With -allow-empty-declarations: empty structs allowed in variable declarations func initializeConfig() { var config = Config{} // OK: empty struct in variable declaration config := Config{} // OK: empty struct in short variable declaration ptr := &Config{} // OK: empty struct in pointer declaration _ = config _ = ptr } func createConfig() Config { return Config{} // ERROR: empty struct in return statement } func processConfigs() []Config { return []Config{{}} // ERROR: empty struct in slice literal } ``` -------------------------------- ### Error Handling for Return Values in Go Source: https://github.com/gaijinentertainment/go-exhaustruct/blob/master/README.md Explains how Go Exhaustruct handles errors when non-pointer types are returned along with errors. The tool ignores non-error types if the return statement includes a non-nil value that satisfies the `error` interface. Examples show cases where errors are suppressed and where they are raised. ```go package main import ( "errors" ) type Shape struct { Length int Width int } func NewShape() (Shape, error) { return Shape{}, errors.New("error") // will not raise an error } type MyError struct { Err error } func (e MyError) Error() string { return e.Err.Error() } func NewSquare() (Shape, error) { return Shape{}, &MyError{Err: errors.New("error")} } func NewCircle() (Shape, error) { return Shape{}, &MyError{} // will raise "main.MyError is missing field Err" } ``` -------------------------------- ### Global Empty Struct Allowance (Shell) Source: https://github.com/gaijinentertainment/go-exhaustruct/blob/master/README.md Shows how to use the -allow-empty flag with exhaustruct to permit all empty struct initializations globally. This is useful for configuration structs with default values. ```shell exhaustruct -allow-empty ./... ``` -------------------------------- ### Return Statement Empty Struct Allowance (Shell) Source: https://github.com/gaijinentertainment/go-exhaustruct/blob/master/README.md Demonstrates using the -allow-empty-returns flag to allow empty structs only within function return statements. This is useful for handling error conditions gracefully. ```shell exhaustruct -allow-empty-returns ./... ```