Golang Min



Handling 1 Million Requests per Minute with Go Jul 6, 2015. Here at Malwarebytes we are experiencing phenomenal growth, and since I have joined the company over 1 year ago in the Silicon Valley, one my main responsibilities has been to architect and develop several systems to power a fast-growing security company and all the needed infrastructure to support a product that is used by millions. Overview math package of GO provides a Min method that can be used to get the minimum of two numbers.

Golang Min Max

We will use the MinIO server running at in this example. Feel free to use this service for testing and development. Access credentials shown in this.

Introduction:

Go has only one looping construct, the for loop.

Learning with some examples:

Simple for loop.

The output will be:

Golang Minio

If we try to loop with 2 variables.

Golang

The output will be an error:

So to remove this error:

The output will be:

We can change the scope of i from for loop to main function()

The output will be:

When we remove increment and put inside loop.

Then also the output will remain same.

We can also remove the condition on for loop syntax and put it inside it.

The output will be still the same:

We can also use continue.

The output will be:

We can use nested loops.

The output will be:

So we want to break out of 2 loops after we found the first '3' integer., we need to use a Label.

The Loop: which is created before for loop is the label and that same need to be added after a break to indicate where we want our program. The output is:

To use any collection in for loop, we need to use a range loop.

As we toke the example of slice, the output will be:

If we only want keys then:

If we only want values then:

Summary:

  • For statements can have the syntax as:
  1. for initializer; test; increment{}
  2. for test {}
  3. for {}
  • We can exit early using break, continue and labels.
  • We can loop over arrays, maps, slices, strings and channels using for k, v := range collection {}

Golang Min Function

Hope it would be helpful to you!