Skip to content

Instantly share code, notes, and snippets.

@MrUPGrade
Last active April 13, 2020 11:01
Show Gist options
  • Save MrUPGrade/aa643f082e49b2e36f9b46669f19f2ca to your computer and use it in GitHub Desktop.
Save MrUPGrade/aa643f082e49b2e36f9b46669f19f2ca to your computer and use it in GitHub Desktop.
Interfaces for inner objects in the structure
package main
import "fmt"
type (
Inner struct {
Name string
}
Root struct {
Obj1 Inner
Obj2 Inner
}
InnerInterface interface {
SetName(string)
}
)
func (i *Inner) SetName(name string) {
i.Name = name
}
func SetName(innerInterface InnerInterface) {
innerInterface.SetName("new name")
}
func main() {
r := Root{
Obj1: Inner{Name: "1"},
Obj2: Inner{Name: "2"},
}
SetName(&r.Obj1)
fmt.Println(r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment