Created
January 20, 2021 16:22
-
-
Save afrinjamanbd/45942f4d7edb6fdf03fe1d96385566e6 to your computer and use it in GitHub Desktop.
A builder design pattern or chaining in Golang.
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 Calculator struct{ | |
total int | |
} | |
func (calculate Calculator) add(num int) Calculator { | |
calculate.total = calculate.total + num | |
return calculate | |
} | |
func (calculate Calculator) remove(num int) Calculator { | |
calculate.total = calculate.total - num | |
return calculate | |
} | |
func (calculate Calculator) calc() int{ | |
return calculate.total | |
} | |
func main() { | |
obj:= Calculator{total: 0} | |
//obj.add(2).remove(1).calc() | |
fmt.Println(obj.add(12).remove(1).calc()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A builder design pattern or chaining in Golang.