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 Boolean values


In this section, we're going to see how to convert a string value into a Boolean value:

  1. So, in our editor, we're going to have a variable name isNew, and this is going to be a string value and is a true value. We're going to use a package called strconv, which has ParseBool. It returns two things: one is the Boolean value and the other is an error. So, let's check the following code:
package main
import (
  "strconv"
  "fmt"
)
func main(){
  isNew := "true"
  isNewBool, err := strconv.ParseBool(isNew)
  if(err != nil){
    fmt.Println("failed")
  }else{
    if(isNewBool){
      fmt.Print("IsNew")
    }else{
      fmt.Println("Not new")
    }
  }
}
  1. You should check whether an error is not nil. This will mean that an error occurred and we will have to handle it. For example, we're just going to output some failure message, that is, failed.
  2. If it's not nil in other languages, but it's nil here, then we're going to have to check the isNew Boolean. If it looks...