June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
58
Task/Extend-your-language/Go/extend-your-language.go
Normal file
58
Task/Extend-your-language/Go/extend-your-language.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type F func()
|
||||
|
||||
type If2 struct {cond1, cond2 bool}
|
||||
|
||||
func (i If2) else1(f F) If2 {
|
||||
if i.cond1 && !i.cond2 {
|
||||
f()
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func (i If2) else2(f F) If2 {
|
||||
if i.cond2 && !i.cond1 {
|
||||
f()
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func (i If2) else0(f F) If2 {
|
||||
if !i.cond1 && !i.cond2 {
|
||||
f()
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func if2(cond1, cond2 bool, f F) If2 {
|
||||
if cond1 && cond2 {
|
||||
f()
|
||||
}
|
||||
return If2{cond1, cond2}
|
||||
}
|
||||
|
||||
func main() {
|
||||
a, b := 0, 1
|
||||
if2 (a == 1, b == 3, func() {
|
||||
fmt.Println("a = 1 and b = 3")
|
||||
}).else1 (func() {
|
||||
fmt.Println("a = 1 and b <> 3")
|
||||
}).else2 (func() {
|
||||
fmt.Println("a <> 1 and b = 3")
|
||||
}).else0 (func() {
|
||||
fmt.Println("a <> 1 and b <> 3")
|
||||
})
|
||||
|
||||
// It's also possible to omit any (or all) of the 'else' clauses or to call them out of order
|
||||
a, b = 1, 0
|
||||
if2 (a == 1, b == 3, func() {
|
||||
fmt.Println("a = 1 and b = 3")
|
||||
}).else0 (func() {
|
||||
fmt.Println("a <> 1 and b <> 3")
|
||||
}).else1 (func() {
|
||||
fmt.Println("a = 1 and b <> 3")
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue