Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Struct Embedding

This Go program demonstrates the concept of composition in Go structs, where a struct includes another struct as a field. It also showcases method overriding through embedding and interface implementation. Let's go through each part of the code with inline comments and additional explanations:

package main

import "fmt"

// Define a struct named 'base' with a field 'num'
type base struct {
    num int
}

// Method 'describe' for the 'base' struct, returns a string describing the base struct
func (b base) describe() string {
    return fmt.Sprintf("base with num=%v", b.num)
}

// Define a struct named 'container' that embeds the 'base' struct and has an additional field 'str'
type container struct {
    base // Embedding the 'base' struct
    str  string
}

func main() {
    // Create an instance of the 'container' struct with field values
    co := container{
        base: base{
            num: 1,
        },
        str: "some name",
    }

    // Print the values of the 'container' struct fields
    fmt.Printf("co={num: %v, str: %v}\n", co.num, co.str)

    // Access the 'num' field of the embedded 'base' struct directly
    fmt.Println("also num:", co.base.num)

    // Call the 'describe' method on the 'container' struct, which internally calls the 'describe' method of the embedded 'base' struct
    fmt.Println("describe:", co.describe())

    // Define an interface named 'describer' with a 'describe' method
    type describer interface {
        describe() string
    }

    // Create a variable 'd' of type 'describer' and assign the 'container' struct to it
    var d describer = co

    // Call the 'describe' method on the 'describer' interface, which calls the overridden method in the 'container' struct
    fmt.Println("describer:", d.describe())
}

Output

co={num: 1, str: some name}
also num: 1
describe: base with num=1
describer: base with num=1

Explanation:

  1. The base struct has a single field, num, and a method called describe that returns a string describing the base struct.

  2. The container struct embeds the base struct, effectively inheriting its fields and methods. It also has an additional field, str.

  3. In the main function:

    • An instance co of the container struct is created with field values.

    • Field values of the container struct are printed, and the embedded base struct field (num) is accessed directly.

    • The describe method is called on the container struct, which internally calls the describe method of the embedded base struct.

    • An interface named describer is defined with a describe method.

    • A variable d of type describer is created and assigned the container struct.

    • The describe method is called on the describer interface, demonstrating method overriding. The overridden method in the container struct is called.

PreviousInterfacesNextGenerics

Last updated 1 year ago

Was this helpful?