Created
December 24, 2022 15:22
-
-
Save aasumitro/d9e14883bdc21471d9598b7692a20225 to your computer and use it in GitHub Desktop.
Design Pattern in GO
This file contains hidden or 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" | |
| func main() { | |
| dummy := NewDummy() | |
| fmt.Println(dummy.Lorem) | |
| fmt.Println(dummy.Ipsum) | |
| dummy.Lorem = "test" | |
| dummy.Ipsum = "test2" | |
| fmt.Println(dummy.Lorem) | |
| fmt.Println(dummy.Ipsum) | |
| dummy2 := NewDummy(Lorem("lorem"), Ipsum("ipsum")) | |
| fmt.Println(dummy2.Lorem) | |
| fmt.Println(dummy2.Ipsum) | |
| } | |
| type Dummy struct { | |
| Lorem string | |
| Ipsum string | |
| } | |
| type Option func(*Dummy) | |
| func Lorem(lorem string) Option { | |
| return func(dummy *Dummy) { | |
| dummy.Lorem = lorem | |
| } | |
| } | |
| func Ipsum(ipsum string) Option { | |
| return func(dummy *Dummy) { | |
| dummy.Ipsum = ipsum | |
| } | |
| } | |
| func NewDummy(opts ...Option) *Dummy { | |
| dummy := &Dummy{} | |
| for _, opt := range opts { | |
| opt(dummy) | |
| } | |
| return dummy | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment