Исходники и презентации
This commit is contained in:
42
lessons/errors/optional/main.go
Normal file
42
lessons/errors/optional/main.go
Normal file
@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var NullOptional = Optional[int]{}
|
||||
|
||||
type Optional[T any] struct {
|
||||
value T
|
||||
present bool
|
||||
}
|
||||
|
||||
func NewOptional[T any](value T) Optional[T] {
|
||||
return Optional[T]{
|
||||
value: value,
|
||||
present: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Optional[T]) HasValue() bool {
|
||||
return o.present
|
||||
}
|
||||
|
||||
func (o *Optional[T]) Value() T {
|
||||
return o.value
|
||||
}
|
||||
|
||||
func divide(lhs, rhs int) Optional[int] {
|
||||
if rhs == 0 {
|
||||
return NullOptional
|
||||
}
|
||||
|
||||
result := lhs / rhs
|
||||
return NewOptional(result)
|
||||
}
|
||||
|
||||
func main() {
|
||||
x := 100
|
||||
y := 0
|
||||
|
||||
optional := divide(x, y)
|
||||
fmt.Println(optional)
|
||||
}
|
||||
Reference in New Issue
Block a user