Skip to content

Instantly share code, notes, and snippets.

@WesleyBatista
Created December 9, 2019 22:48
Show Gist options
  • Save WesleyBatista/e271526795d640aa5ba5975e25d690e9 to your computer and use it in GitHub Desktop.
Save WesleyBatista/e271526795d640aa5ba5975e25d690e9 to your computer and use it in GitHub Desktop.
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