Skip to content

Instantly share code, notes, and snippets.

@thanhhh
Created May 19, 2018 11:00
Show Gist options
  • Save thanhhh/669e671c383337c891ab309f531b35d4 to your computer and use it in GitHub Desktop.
Save thanhhh/669e671c383337c891ab309f531b35d4 to your computer and use it in GitHub Desktop.
Check dependency injection instances between parent and child
// https://play.golang.org/p/FNyxFKcizua
package main
import (
"fmt"
)
func main() {
s := "Hello"
b := Base{Logger: &s}
fmt.Printf("Base's Logger: %p\n",b)
r := NewRepository(b)
fmt.Printf("Repository: %p\n", r)
fmt.Printf("Repository's Logger: %p\n", r.Logger)
r2 := NewRepository(b)
fmt.Printf("Repository2: %p\n", r2)
fmt.Printf("Repository2's Logger: %p\n", r2.Logger)
}
type Base struct {
Logger *string
}
type Repository struct {
Base
Value int
}
func NewRepository(b Base) *Repository {
return &Repository{
Base: b,
}
}
// Base's Logger: %!p(main.Base={0x1040c128})
// Repository: 0x1040c138
// Repository's Logger: 0x1040c128
// Repository2: 0x1040c140
// Repository2's Logger: 0x1040c128
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment