github.com/enetx/g@v1.0.80/examples/monads/result.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/enetx/g" 8 ) 9 10 func main() { 11 // Example 1: Chaining operations on a Result using Then 12 13 // Define a function to double an integer 14 double := func(x g.Int) g.Result[g.Int] { return g.Ok(x * 2) } 15 16 // Create a Result containing the integer 10, then double it twice using Then 17 result := g.Ok(g.Int(10)).Then(double).Then(double) 18 19 // Check if the result is an Ok value or an error 20 if result.IsOk() { 21 // Print the value if it's an Ok result 22 fmt.Println("Result:", result.Ok()) // Output: Result: 40 23 } else { 24 // Print the error if it's an error result 25 fmt.Println("Error:", result.Err()) 26 } 27 28 // Example 2: Chaining multiple operations on a Result using Then 29 30 // Define a function to square an integer 31 square := func(x g.Int) g.Result[g.Int] { return g.Ok(x * x) } 32 33 // Define a function to add five to an integer 34 addFive := func(x g.Int) g.Result[g.Int] { return g.Ok(x + 5) } 35 36 // Chain square and addFive operations on a Result containing the integer 10 37 result = g.Ok(g.Int(10)).Then(square).Then(addFive) 38 39 // Check if the result is an Ok value or an error 40 if result.IsOk() { 41 // Print the value if it's an Ok result 42 fmt.Println("Result:", result.Ok()) // Output: Result: 105 43 } else { 44 // Print the error if it's an error result 45 fmt.Println("Error:", result.Err()) 46 } 47 48 // Example 3: Converting a string to an integer and then doubling the result 49 50 // Convert the string "15" to an integer and then double it 51 result = g.String("15").ToInt().Then(double) 52 53 // Check if the result is an Ok value or an error 54 if result.IsOk() { 55 // Print the value if it's an Ok result 56 fmt.Println("Result:", result.Ok()) // Output: Result: 30 57 } else { 58 // Print the error if it's an error result 59 fmt.Println("Error:", result.Err()) 60 } 61 62 // Example 4: Handling division by zero 63 64 // Define a function to divide 10.0 by a float64 value, handling division by zero 65 divideByZero := func(x float64) g.Result[float64] { 66 if x == 0 { 67 return g.Err[float64](fmt.Errorf("division by zero")) 68 } 69 return g.Ok(10.0 / x) 70 } 71 72 // Attempt to divide 10.0 by 0.0 73 resultFloat := g.Ok(0.0).Then(divideByZero) 74 75 // Check if the result is an Ok value or an error 76 if resultFloat.IsOk() { 77 // Print the value if it's an Ok result 78 fmt.Println("Result:", resultFloat.Ok()) 79 } else { 80 // Print the error if it's an error result 81 fmt.Println("Error:", resultFloat.Err()) // Output: Error: division by zero 82 } 83 84 // Example 5: Converting a string to an integer using MapResult and MapToResult 85 86 // Define a string containing a valid integer 87 str := "123" 88 89 // Create a Result containing the string 90 strResult := g.Ok(str) 91 92 // Use ResultMap to convert the string to an integer 93 intResult := g.ResultMap(strResult, func(s string) g.Result[int] { return g.ResultOf(strconv.Atoi(s)) }) 94 95 // Alternatively, use ResultOfMap to convert the string to an integer 96 // This simplifies the process by directly passing strconv.Atoi 97 intResult = g.ResultOfMap(strResult, strconv.Atoi) 98 99 // Check if the intResult is an error or contains a value 100 if intResult.IsErr() { 101 fmt.Println("Error:", intResult.Err()) 102 } else { 103 fmt.Println("Integer Value:", intResult.Ok()) 104 } 105 }