Go error checking and chaining - go

package main
import (
"errors"
"fmt"
)
type FieldElement struct {
Num int
Prime int
}
func (el *FieldElement) Add(o FieldElement) (FieldElement, error) {
if el.Prime != o.Prime {
return FieldElement{}, errors.New("cannot operate on two numbers in different Fields")
}
return FieldElement{(el.Num + o.Num) % el.Prime, el.Prime}, nil
}
// Sub, Mul, Div and Pow also members of FieldElement returning (FieldElement, error)
func main() {
a := FieldElement{13, 19}
b := FieldElement{9, 19}
fmt.Println(a.Add(b))
}
type Point struct {
X FieldElement
Y FieldElement
A FieldElement
B FieldElement
}
Now upon Point creation I want to check if a point is on the curve defined as y^2 = x^3 + ax + b
Each operation of FieldElement returns 2 values to satisfy the error checking.
So I can not do
x.Pow(3).Add(a.Mul(x)).Add(b)
and end up with
pow3, e1 := x.Pow(three)
mul, e2 := a.Mul(x)
add, e3 := pow3.Add(mul)
rval, e4 := add.Add(b)
// check that y^2 == rval
This makes the code hard to follow, what is the golang way to properly do this?
playground

Make your methods return only a single value, so they can be chained, and store the error value in that returned value, to check after. Example:
type FieldElement struct {
Num int
Prime int
Error error
}
func (el *FieldElement) Add(o FieldElement) *FieldElement {
if el.Error != nil {
// An error already happened, so do nothing
return el
}
if el.Prime != o.Prime {
el.Error = errors.New("cannot operate on two numbers in different Fields")
} else {
el.Num = (el.Num + o.Num) & el.Prime
}
return el
}
Then use it like this:
x.Add(...).Mul(...).Sub(...) // etc
if x.Error != nil {
// handle error here
}

Related

Operate on a struct with lots of pointers (especially to numbers)

I have quite a few data structures that contain mostly numbers, I get the data, do a calculation and return the result.
The thing is that all of those numbers can be zero and hence, I had to switch to using pointers (*int64 or *float64) so that the default is nil and not 0.
Unfortunately, I don't know of a solution to this in Go except using pointers.
The problem comes now in the Calculate() function that is implemented for all data structures:
type X struct {
A, B, C, D, E, F *int
// and much more
Result *float64
}
func (x *X) Calculate() {
floatptr := func(f float64) *float64 { return &f }
x.Result = floatptr(float64(*x.A + *x.B + *x.C + *x.D + *x.E + *x.F))
}
This function will obviously panic if any of the data is nil. So, I wrote the functions differently that it checks for nil data before the calculation:
func (x *X) CalculateWithNilChecks() {
floatptr := func(f float64) *float64 { return &f }
if x.A == nil || x.B == nil || x.C == nil || x.D == nil || x.E == nil || x.F == nil {
return
}
x.Result = floatptr(float64(*x.A + *x.B + *x.C + *x.D + *x.E + *x.F))
}
The problem is that the data structures are quite long. Having a SUPER long if x != nil looks ugly. I was wondering if there is another (cleaner) way to doing this.
I thought of doing like in the encoding/json and just recover nil pointer dereference panics, not sure if this is cleaner TBH.
Another thought was to reflect the data structures and stop if any of the required data is nil, I don't think this should be necessary for such a simple task.
Here is a playground link for the above code
What am I missing here? Thanks!
As general solution you can unmarshal your JSON into a map of *int pointers or json.RawMessage and then use a reflection to cross check with your struct or just check it with expected number of fields.
func main() {
result := make(map[string]*int)
str := `{ "A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6 }`
json.Unmarshal([]byte(str), &result)
for _, field := range result {
// Check if expect fields exists using reflection or manually
// ...
}
}
You can use reflect module and error when one of the required fields is missing.
Use this as template.
package main
import (
"fmt"
"reflect"
)
type X struct {
A, B, C, D, E, F *int
Result *float64
}
func (x *X) PrintFoo() {
fmt.Println(x.A)
}
func main() {
a := 3
x := X{A: &a}
val := reflect.ValueOf(x)
for i := 0; i < val.Type().NumField(); i++ {
field := val.Type().Field(i)
fieldType := fmt.Sprintf("%s", field.Type)
if fieldType == "*int" && val.FieldByName(field.Name).IsNil() {
fmt.Println("Missing value on field", field.Name)
}
}
}

Modifying receiver with a method on value?

package matrix
import (
"errors"
"strconv"
"strings"
)
// Matrix matrix inteface
type Matrix interface {
Rows() [][]int
Cols() [][]int
Set(r, c, val int) bool
}
// matrix implements the interface Matrix
type matrix struct {
data [][]int
rows int
cols int
}
// New returns a valid matrix created from the input
func New(input string) (Matrix, error) {
var m matrix
rows := strings.Split(input, "\n")
for r, row := range rows {
rowElements := strings.Fields(row)
switch {
case r == 0:
m.rows, m.cols = len(rows), len(rowElements)
matrix, err := allocateMemory(m.rows, m.cols)
if err != nil {
return invalidMatrix()
}
m.data = matrix
case len(rowElements) != m.cols:
return invalidMatrix()
}
for c, element := range rowElements {
element, err := strconv.Atoi(element)
if err != nil {
return invalidMatrix()
}
m.data[r][c] = element
}
}
return m, nil
}
// invalidMatrix returns the error indicating the
// provided matrix is invalid
func invalidMatrix() (Matrix, error) {
return nil, errors.New("invalid matrix")
}
// allocateMemory allocates a 2D slice of int having size RxC
func allocateMemory(R, C int) ([][]int, error) {
if R < 1 || C < 1 {
return nil, errors.New("invalid matrix")
}
matrix := make([][]int, R)
for r := range matrix {
matrix[r] = make([]int, C)
}
return matrix, nil
}
// Set sets the given value at (r,c) in the matrix,
// if (r,c) belongs to the matrix.
func (m matrix) Set(r, c, val int) bool {
switch {
case r < 0 || c < 0:
return false
case r >= m.rows || c >= m.cols:
return false
default:
m.data[r][c] = val
return true
}
}
// order defines the order the matrix to export
// two useful values are columnMajor and rowMajor
type order int
const (
columnMajor order = iota
rowMajor
)
// Cols returns columns of the matrix.
func (m matrix) Cols() [][]int {
return m.export(columnMajor)
}
// Rows returns rows of the matrix.
func (m matrix) Rows() [][]int {
return m.export(rowMajor)
}
// export return the matrix in the required order;
// either columnMajor or rowMajor.
func (m matrix) export(o order) [][]int {
var matrix [][]int
var err error
switch o {
case columnMajor:
matrix, err = allocateMemory(m.cols, m.rows)
if err != nil {
return nil
}
for r, row := range m.data {
for c, element := range row {
matrix[c][r] = element
}
}
case rowMajor:
matrix, err = allocateMemory(m.rows, m.cols)
if err != nil {
return nil
}
for r, row := range m.data {
copy(matrix[r], row)
}
}
return matrix
}
I am having a hard time understanding why the method Set() is able to modify the data of the struct. I had an understanding that methods defined on values cannot do that. I have tried to compare it with another problem where I cannot modify the content of receiver but in this case it just works. A test file for this code is available at test file. Any idea what I am missing?
The reason Set can modify the contents of the slice is that the slice is a reference value. Your other example (in the comment) attempts to assign the field holding the slice, and this won't work - because it's working on a copy. See this code sample:
package main
import (
"fmt"
)
type Holder struct {
s []int
v []int
}
func (h Holder) Set() {
// This will successfully modify the `s` slice's contents
h.s[0] = 99
// This will assign a new slice to a copy of the v field,
// so it won't affect the actual value on which this
// method is invoked.
h.v = []int{1, 2, 3}
}
func main() {
var h Holder
h.s = []int{10, 20, 30}
h.v = []int{40, 50, 60}
fmt.Println("before Set:", h)
h.Set()
fmt.Println("after Set:", h)
}
You can run it on the playground, and it prints:
before Set: {[10 20 30] [40 50 60]}
after Set: {[99 20 30] [40 50 60]}
What happens here is that even though Set gets a copy of h, and hence h.s is a copy too, but both copies point to the same underlying slice, so the contents can be modified. Read this post for all the details.
A slice value contains (ptr, len, cap) where ptr is a pointer to the slice's underlying array. The Set method modifies the slice's underlying array by dereferencing the pointer. The slice value, stored in the field, is not modified.
The Go Language blog post on slices describes the slice memory layout in more detail.

Can't trace the problem in BST delete function

I can't trace the mistake in my logic in BST delete function in Go.
func delete(d *Node, v int) {
if d == nil {
fmt.Println("The tree is empty")
}
if v < d.key {
delete(d.left, v)
} else if v > d.key {
delete(d.right, v)
} else if v == d.key {
if d.right == nil && d.left == nil {
d = nil
} else {
if d.left == nil && d.right != nil {
d.key= d.right.key
delete(d.right,d.key)
} else if d.right == nil && d.left != nil {
d.key= d.left.key
delete(d.left,d.key)
}else{
min := minvalue(d.right)
d.key = min.key
delete(d.right, min.key)
}
}
}
}
The output shouldn't contain 4 but the result is instead showing 6 two times
The expected output is 5 6, but it's showing 4 6 6
As several have noted in the comments, you should provide a Minimal Reproducible Example, which helps people avoid wasting time on simple misunderstandings.
In this case, though, it's pretty obvious what you have done wrong—at least, at the first layer. (There may be more, depending on what you intend to do with these trees.)
Consider the following function:
func setToNil(p *int) {
p = nil
}
Let's use this from a main:
func main() {
x := 3
px := &x
fmt.Println("before: x =", x, "px =", px)
setToNil(px)
fmt.Println("after: x =", x, "px =", px)
}
(Complete version on the Go playground)
What do you expect this program to do? Try it out: did it do what you expected? Why, or why not? If not, what about this variant:
func setToTheAnswer(i int) {
i = 42
}
func main() {
x := 3
fmt.Println("before: x =", x)
setToTheAnswer(x)
fmt.Println("after: x =", x)
}
Fill in the rest and try it out. Why didn't x change? (Should it have changed? If you think so, why do you think so? The language definition says that it should not.)
Now, compare that to this version:
func setToTheAnswer(p *int) {
*p = 42
}
func setToNil(q **int) {
*q = nil
}
func main() {
x := 3
px := &x
fmt.Println("before: x =", x, "px =", px)
setToTheAnswer(px)
setToNil(&px) // note the & in front of px
fmt.Println("after: x =", x, "px =", px)
}
What will this version do? Try it on the playground.
With that in mind, think about your variable d
Your function:
func delete(d *Node, v int) {
// ...
}
takes a parameter named d of type pointer to Node (and v of type int of course). If you change d in delete, that has no effect on any * Node variable in any caller, because d is a copy of this pointer-to-Node. You can change *d to change the Node to which the caller's pointer points, but you cannot change the caller's pointer.
There are multiple different ways to fix this. For instance, instead of taking a d *Node you might take a different object that contains a root *Node pointer, or you might take a pd **Node so that you can update a d *Node in the caller. Which is the right way? That's up to you.

undefined <type float32 has no field or method sum> error.How do I fix it?

this is my program.When I run it, it gives the following error - a.sum undefined (type float32 has no field or method sum)
package main
import (
"fmt"
)
type Calculation interface {
operation(input []float32)
}
type Addition struct {
sum float32
}
func (a Addition) operation(input []float32) {
a.sum = input[0]
for _, a := range input[1:] {
a.sum += a
}
fmt.Println("Sum :", a.sum)
}
func main() {
var n int
fmt.Println("Enter the no of inputs : ")
fmt.Scanln(&n)
var input []float32
input = make([]float32 , n)
fmt.Println("Enter the numbers ")
for i:=0 ; i <n ; i++ {
fmt.Scanln(&input[i])
}
var c Calculation
i := Addition{0}
c = i
c.operation(input)
}
I have written 3 more functions Subtraction , Multiplication and Division with Addition. All of them follow similar format but those three run with out any error, Only addition is giving this error. Unable to figure out why.
Your variable a in the loop shadows the variable a representing Addition. Changing your loop to this will solve the problem:
for _, v := range input[1:] {
a.sum += v
}

Go: best practice for safely converting integers to protocol buffer enum values

I have an enum in a proto file that generates to integer constants in the pb.go file. I now have some integers coming from the an external data source and want to safely map them to the possible constants.
Here is what I currently have: https://play.golang.org/p/-5VZqPbukd
package main
import (
"errors"
"fmt"
)
//enum in the proto file
//
// enum X {
// A = 0;
// B = 1;
// C = 2;
// D = 3;
// }
//enum type generated by protoc
type X int32
//enum constants generated by protoc
const (
X_A X = 0
X_B X = 1
X_C X = 2
X_D X = 3
)
func intToX(v int) (X, error) {
x := X(v)
switch x {
case X_A, X_B, X_C, X_D:
return x, nil
}
return 0, errors.New("could not convert int to X")
}
func main() {
for i := -1; i < 10; i++ {
if x, err := intToX(i); err != nil {
fmt.Println("unhandled error:", err, "for input value", i)
} else {
fmt.Printf("%d => X(%d)\n", i, x)
}
}
}
Question: Is there a better, more idiomatic way to map incoming integer values to protoc-generated constants?
In particular, I would like to avoid listing all constants explicitly in the case A, B, C, D statement.
I do not know which proto generation package you are using, but with github.com/golang/protobuf/proto you also get the reverse mapping of enums.
Example xyz.pb.go generated file:
type TimeInterval int32
const (
TimeInterval_TI_UNKNOWN TimeInterval = 0
TimeInterval_TI_HOUR TimeInterval = 1
TimeInterval_TI_DAY TimeInterval = 2
TimeInterval_TI_WEEK TimeInterval = 3
TimeInterval_TI_MONTH TimeInterval = 4
TimeInterval_TI_QUARTER TimeInterval = 5
TimeInterval_TI_YEAR TimeInterval = 6
)
var TimeInterval_name = map[int32]string{
0: "TI_UNKNOWN",
1: "TI_HOUR",
2: "TI_DAY",
3: "TI_WEEK",
4: "TI_MONTH",
5: "TI_QUARTER",
6: "TI_YEAR",
}
var TimeInterval_value = map[string]int32{
"TI_UNKNOWN": 0,
"TI_HOUR": 1,
"TI_DAY": 2,
"TI_WEEK": 3,
"TI_MONTH": 4,
"TI_QUARTER": 5,
"TI_YEAR": 6,
}
func (x TimeInterval) String() string {
return proto.EnumName(TimeInterval_name, int32(x))
}
func (TimeInterval) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
So with this you could test for existence in the following manner:
if _, found := TimeInterval_name[testinputint]; found{
//all ok
} else {
//not a value for this enum
}
Yes as #RickyA mentions using a range is nice as it verifies for all the possible underlying const values.
Additionally you could check the length of the enum, although that would only be possible when the underlying enum values do not have any 'gaps' and have a consequitive range of numbers.
Verbose explanation via code:
typelength := int32(len(TimeInterval_name))
if testinputint < 0 || int32(testinputint) >= typelength {
// not a value for this enum, return err
}
A bit less verbose, and just using int instead of int32
if testinputint < 0 || int(testinputint) >= len(TimeInterval_name) {
// not a value for this enum, return err
}
But as said, this will only be valid for enums that adhere to a proper iota. That might not be the case when you have changed your enum to read something like this:
var TimeInterval_name = map[int32]string{
0: "TI_UNKNOWN",
1: "TI_HOUR",
2: "TI_DAY",
3: "TI_WEEK",
// we do not use month anymore 4: "TI_MONTH",
5: "TI_QUARTER",
6: "TI_YEAR",
}
as the length of the generated map will be clearly less than six :)
In other words use the found method of #Ricky_A above to keep on the safe side.

Resources