Last active
April 13, 2020 11:01
-
-
Save MrUPGrade/aa643f082e49b2e36f9b46669f19f2ca to your computer and use it in GitHub Desktop.
Interfaces for inner objects in the structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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