Created
December 9, 2019 22:48
-
-
Save WesleyBatista/e271526795d640aa5ba5975e25d690e9 to your computer and use it in GitHub Desktop.
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 Base interface { | |
SetParams(string) | |
GetParams() string | |
} | |
type Object1 struct { | |
A string | |
} | |
type Object2 struct { | |
A string | |
} | |
type Object3 struct { | |
A string | |
} | |
func (o *Object1) SetParams(params string) { | |
o.A = params | |
} | |
func (o *Object2) SetParams(params string) { | |
o.A = params | |
} | |
func (o *Object3) SetParams(params string) { | |
o.A = params | |
} | |
func (o *Object1) GetParams() string { | |
return o.A | |
} | |
func (o *Object2) GetParams() string { | |
return o.A | |
} | |
func (o *Object3) GetParams() string { | |
return o.A | |
} | |
func GetStructByName(name string) Base { | |
switch name { | |
case "one": | |
return &Object1{} | |
case "two": | |
return &Object2{} | |
case "three": | |
return &Object3{} | |
} | |
return nil | |
} | |
func main() { | |
obj1 := GetStructByName("one") | |
obj1.SetParams("klka") | |
fmt.Println(obj1.GetParams()) | |
obj2 := GetStructByName("two") | |
fmt.Println(obj2) | |
obj3 := GetStructByName("three") | |
fmt.Println(obj3) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment