Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid too much conversion

Tags:

go

I have some parts in my current Go code that look like this:

i := int(math.Floor(float64(len(l)/4)))

The verbosity seems necessary because of some function type signatures like the one in math.Floor, but can it be simplified?

like image 545
Sergi Mansilla Avatar asked Dec 19 '12 22:12

Sergi Mansilla


People also ask

What does a high conversion rate indicate?

Your conversion rate is the percentage of visitors to your website that complete a desired goal (a conversion) out of the total number of visitors. A high conversion rate is indicative of successful marketing and web design: It means people want what you're offering, and they're easily able to get it!

What does having a conversion mean?

noun. the act or process of converting; state of being converted. change in character, form, or function. 3. spiritual change from sinfulness to righteousness.

What causes conversion rate to increase?

To increase your conversion rate is to improve your marketing ROI. The more visitors you convert, the higher is the impact you drive on your top line from your existing traffic. The tactics mentioned above are a great place to start to strategically improve your conversion rates.


1 Answers

In general, the strict typing of Go leads to some verbose expressions. Verbose doesn't mean stuttering though. Type conversions do useful things and it's valuable to have those useful things explicitly stated.

The trick to simplification is to not write unneeded type conversions, and for that you need to refer to documentation such as the language definition.

In your specific case, you need to know that len() returns int, and further, a value >= 0. You need to know that 4 is a constant that will take on the type int in this expression, and you need to know that integer division will return the integer quotient, which in this case will be a non-negative int and in fact exactly the answer you want.

i := len(l)/4

This case is an easy one.

like image 163
Sonia Avatar answered Oct 26 '22 20:10

Sonia