Golang tricks

Golang, is fun, and this are some tricks to use

Page content

Golang is a beautiful programming language, this tricks will make it even fun

mixed named and unnamed parameters

Is a known issue, where in a function you return same parameters with names, and you try to return the rest elements without a name for example, if you return the error here without giving it a name, you would run to the same problem

func powerSeries(a int) (square int, cube int, error) {
	square = a * a
	cube = square * a
	//if we need to check for error, and return the error
	return square, cube, nil
}

So you have to chose one of those two

func powerSeries(a int) (square int, cube int, someErrorName error) {
	square = a * a
	cube = square * a
	//if we need to check for error, and return the error
	return square, cube, nil
}

OR

func powerSeries(a int) (int,int, error) {
	square = a * a
	cube = square * a
	//if we need to check for error, and return the error
	return square, cube, nil
}