-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added new function to main.go #2
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ServiceWeaver/weaver" | ||
) | ||
|
||
// Reverser component. | ||
type Mathserve interface { | ||
Add(context.Context, int, int) (int, error) | ||
Sub(context.Context, int, int) (int, error) | ||
} | ||
|
||
type mathserve struct { | ||
weaver.Implements[Mathserve] | ||
} | ||
|
||
func (m *mathserve) Add(_ context.Context, x, y int) (int, error) { | ||
if x == 0 || y == 0 { | ||
return 0, fmt.Errorf("Zero values") | ||
} | ||
|
||
result := 0 | ||
|
||
for i := 0; i < x; i++ { | ||
result += y | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (m *mathserve) Sub(_ context.Context, x, y int) (int, error) { | ||
if x == 0 || y == 0 { | ||
return 0, fmt.Errorf("Zero values") | ||
} | ||
|
||
result := 0 | ||
|
||
for i := 0; i < x; i++ { | ||
result -= y | ||
} | ||
|
||
return result, nil | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a Go code implementing an interface
Overall, the code seems to be functioning correctly and just needs some minor improvements. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code patch looks like it adds two new endpoints
/add
and/sub
, which use themathserve
declared earlier to perform basic addition and subtraction operations.One improvement suggestion would be to add proper error handling for the
strconv.Atoi()
calls, as they could potentially return errors if the input string is not a valid integer.Additionally, it would be helpful to rename
num
to something more descriptive, such asresult
, to make the code easier to understand.