Skip to content

Instantly share code, notes, and snippets.

@afrinjamanbd
Created January 20, 2021 16:22
Show Gist options
  • Save afrinjamanbd/45942f4d7edb6fdf03fe1d96385566e6 to your computer and use it in GitHub Desktop.
Save afrinjamanbd/45942f4d7edb6fdf03fe1d96385566e6 to your computer and use it in GitHub Desktop.
A builder design pattern or chaining in Golang.
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())
}
@afrinjamanbd
Copy link
Author

A builder design pattern or chaining in Golang.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment