Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

Introduction

The code challenging platforms like HackerRank or CodeEval test the logical ability and programming ability of a coder. Dynamic languages give us the edge in developing programs faster. The algorithms which require huge computation may lead to timeouts with interpreters. Go is a compiled and disciplined language which can do very well in the competitive programming world. Beginners always feel challenged when they start using Go because it is so strict. At the same time, it is so sweet with type inferences and out-of-box, built-in library.

These are essential things you need to know before jumpstart coding in Golang.

Input and Output

Even though many tutorials describe many ways to read input and print output, beginners of Golang usually find it hard to start. So I will cover the basic input/output pattern in Golang.

We can also convert an Integer, Float value to a string using Iota method of strconv:

i := 10
strVal, _ = strconv.Iota(i)

If we observe carefully, strconv is returning two values, not one. The first value is the actual result, while "_" contains the error message if the operation fails. If we plan to handle the error wisely then we can use it. If the operation is successful, it stores nil into err.

i := 10
// Now you can use err if something goes wrong
strVal, err = strconv.Iota(i)

Make Array Slices Your Great Friend

In other programming languages, Arrays and Lists are the main storing data structures. But in Go, you must use slices. Here, the frequent usage of slices is essential in competitive programming.

So we have two methods for splitting and stripping of strings: strings.TrimSpace(string) and strings.Split(trimmed_stream, seperator).

These are equivalent to Python's trim() and split() built-in methods

Other Tricks

Using Math library

While using Golang's math library, you should be careful about the types of arguments passed and the type of the operation result. For example, for finding the absolute value of a number, you need to pass a float64 number to the math.Abs function.