Book Image

Hands-On Go Programming

By : Tarik Guney
Book Image

Hands-On Go Programming

By: Tarik Guney

Overview of this book

<p>With its C-like speed, simplicity, and power for a growing number of system-level programming domains, Go has become increasingly popular among programmers. Hands-On Go Programming teaches you the Go programming by solving commonly faced problems with the help of recipes. You will start by installing Go binaries and get familiar with the tools used for developing an application. Once you have understood these tasks, you will be able to manipulate strings and use them in built-in function constructs to create a complex value from two floating-point values. You will discover how to perform an arithmetic operation date and time, along with parsing them from string values. In addition to this, you will cover concurrency in Go, performing various web programming tasks, implementing system programming, reading and writing files, and honing many fundamental Go programming skills such as proper error handling and logging, among others. Whether you are an expert programmer or newbie, this book helps you understand how various answers are programmed in the Go language.</p>
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributor
Preface
Index

Parsing string values to integer and float types


In this section, we're going to see how to parse string values to integer and float types.

Parsing string values to integer types

Let's imagine that we have a variable called number, which has 2 as a string. We're going to use strconv.ParseInt and it returns two variables: the first one is the actual integer that we are expecting and the other is the return variable that arises if any error occurs during conversion.

 

 

If you look at the signature, you'll see that it returns integer 64 and an error:

So, the first thing we can do is check whether any error occurred during conversion; if it is not nil, we can understand that something happened and we'll type Error happened.

Note

There's no try...catch in Go, so you always have to do error checking if you want to write resilient code.

Now, for if checking, we can give Success as an output if the number is 2. Now, let's run the described code as follows:

package main
import (
  "strconv"
  "fmt"
)
func...