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

Adding and subtracting from a date


In this section, we're going to learn how to add and subtract from a date value.

Adding to the date

Let's go ahead and learn how to add one month to the current date. But before doing that, we'll need to know the current date. You can do this by following the procedure that we learned in our previous section. Consider that I got 8th August (2018-08-08 09:35:16.2687997 +0530 IST m=+0.003951601) as the output and we have to add one more month to this value. By using the AddDate function on the  time type, we can add as many years, months, and days as we want, since it accepts three parameters. This is how the entire code will look:

package main

import (
  "time"
  "fmt"
)

func main(){
  current := time.Now()
  septDate := current.AddDate(0,1,0)

  fmt.Println(current.String())
  fmt.Println(septDate.String())
}

So, looking at the following screenshot of the output, you will notice that we have successfully added one extra month to August by passing the value...