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

Converting a Boolean into a String


We're going to start off with learning how to convertBoolean value into a String value:

  1. In our editor, after creating a new file, main.go, and the main function, as always, let's consider a variable called isNew, which is a Boolean. Hence the value will be true.

 

  1. So, let's say that we want to print it out to our console with a message. Check the following screenshot:

As you can see, we encounter a compile-time error. Thus, you cannot use the + operator and we need to convert the isNew Boolean value to its string representation.

  1. Let's use the stringconvert package, which has various string conversion functions, of which, we're going to use FormatBool.
  2. Taking a Boolean value returns every string representation of it, and in this case, it's isNew. If you look at the signature, you'll see that it returns true or false based on the value of the Boolean value passed:
  1. So, let's add isNewStr, run it and check the output:

There is another way of printing such values...