Skip to content

Instantly share code, notes, and snippets.

@napicella
Last active April 1, 2018 14:43
Show Gist options
  • Save napicella/5082912ca89847629bf33f8199a1dfc9 to your computer and use it in GitHub Desktop.
Save napicella/5082912ca89847629bf33f8199a1dfc9 to your computer and use it in GitHub Desktop.
Golang-patterns maybe
Context("User present", func() {
var greeting string
MaybeUser(getUser(1)).IfPresent(func(u *User) {
greeting = "Hello " + u.name
})
It("greets the user", func() {
Expect(greeting).To(Equal("Hello Mickey"))
})
})
Context("User absent", func() {
var greeting string
MaybeUser(getUser(-1)).WhenAbsent(func() {
greeting = "Hello stranger"
})
It("greets the user", func() {
Expect(greeting).To(Equal("Hello stranger"))
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment