using Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/using/ Fri, 08 Jan 2021 16:27:42 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 using Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/using/ 32 32 159787465 Understand If Else Statement in Go (Golang) https://vikasboss.github.io/understand-if-else-statement-golang/ https://vikasboss.github.io/understand-if-else-statement-golang/#comments Sat, 02 May 2020 09:23:33 +0000 https://vikasboss.github.io/?p=2088 This is the  chapter 12 of the golang comprehensive tutorial series. Refer to this link for other chapters of the series – Golang Comprehensive Tutorial Series Next Tutorial – SwitchPrevious Tutorial – For Range loop Now...

The post Understand If Else Statement in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
This is the  chapter 12 of the golang comprehensive tutorial series. Refer to this link for other chapters of the series – Golang Comprehensive Tutorial Series

Next Tutorial – Switch
Previous Tutorial – For Range loop

Now let’s check out the current tutorial. Below is the table of contents for current tutorial.

Overview

Go has if-else statement similar to any other programming language to perform the basic conditional logic. Below is the format for if-else statement in golang

if condition {
   //Do something
} else if condition {
   //Do something
} else {
   //Do something

Before we move further let’s talk about the condition first. Only a statement or a combination of statements that result in a boolean are allowed for a condition in if. false boolean is treated as false in a condition in go and true boolean is treated as true. As mentioned above, the condition can be composed of multiple statements combined by operators in Go such as &&, ||, >, <, >=, <=, ! etc.

Now let’s look into the if-else statement in detail to understand the small things. Go supports below formats for the if-else statement

  • Only if
  • If Else
  • If Else Ladder
  • Nested if-else
  • If with a short statement

If statement

if statement alone has below format

if condition {
   //Do something
}

If the condition is true then the statement inside the braces is executed.  Some points to note about if statement

  • Brackets can be omitted around the condition.
  • Opening and closing braces after condition are mandatory

Let’s see a working example. Below program checks if a number is greater than 5.

package main

import "fmt"

func main() {
    a := 6
    if a > 5 {
        fmt.Println("a is greater than 5")
    }
}

Output

a is greater than 5

Let’s see another example of multiple statement in a if condition. Below is a program to check if a number lies in a particular range. Notice that multiple statement in the condition are joined by the && operator.

package main

import "fmt"

func main() {
    a := 4
    if a > 3 && a < 6 {
        fmt.Println("a is within range")
    }
}

Output

a is within range

If Else Statement

If Else statement has below format

if condition {
   //Do something
} else {
   //Do something
}

If the condition is true then the statement inside the if block is executed otherwise the statement inside the else block is executed. Some points to note about if-else statement.

  • The else keyword should be on the same line as the closing brace for it. If not there will be below compiler error.
syntax error: unexpected else, expecting }

Let's see a small example of if else statement. In below program we use if else statement to figure out the max number of 2 numbers

package main

import "fmt"

func main() {
    a := 1
    b := 2

    if a > b {
        fmt.Println("a is greater than b")
    } else {
        fmt.Println("b is greater than a")
    }
}

Output

b is greater than a

If Else Ladder

If Else ladder has the below format

if condition1 {
   //Do something
} else if condition2 {
   //Do something
} else {
  //Do something
}

Some points to note about this if else ladder

  • Any number of else if statement can be added in the middle
  • else if should lie on the same line as the previous closing brace

Below is a working code example. The code given an age is using a if else ladder to find out weather a person is "Kid", "Young" or "Old".

package main

import "fmt"

func main() {
    age := 29
    if age < 18 {
        fmt.Println("Kid")
    } else if age >= 18 && age < 40 {
        fmt.Println("Young")
    } else {
        fmt.Println("Old")
    }
}

Output:

Young

Nested If Else

Below are some one of the possible format for nested if else.

Only nested if

if condition {
  //Do something
  if condition2 { 
    //Do something
  }
  //Do something
}

Nested if else

if condition1 {
   //....
   if condition2 {
      //...
   } else {
      //...
   }
  //...
}

Below combination is also possible for nested if else

if condition1 {
   //...
} else {
   //...
   if condition2 {
      //...
   } else {
     //....
   }
   //....
}

Let's see a working example of nested if else. In below program we print the max of three numbers using nested if else.

package main

import "fmt"

func main() {
    a := 1
    b := 2
    c := 3
    if a > b {
        if a > c {
            fmt.Println("Biggest is a")
        } else if b > c {
            fmt.Println("Biggest is b")
        }
    } else if b > c {
        fmt.Println("Biggest is b")
    } else {
        fmt.Println("Biggest is c")
    }
}

Output:

Biggest is c

If with short statement

If statement also supports a statement before the condition. This statement will be executed before the condition. There can also be new initialized variable in the statement. Below is the format for that.

if statement; condition {
   //Do something
}

The initialization if present in the statement will be a short declaration. Notice that var keyword is not supported in the statement. Let's see a working example

package main

import "fmt"

func main() {
    if a := 6; a > 5 {
        fmt.Println("a is greater than 5")
    }
}

Output

a is greater than 5

The variable that is initialized in if statement is available inside all the branches. As in below example variable a is also available in the else block.

package main

import "fmt"

func main() {
    if a := 1; a > 5 {
        fmt.Println("a is greater than 5")
    } else {
        fmt.Println("a is less than 5")
    }
}

Output:

a is less than 5

If Conditions

We mentioned at the start that only boolean values or statement that result in boolean value are allowed in the if condition. Let's see a working code of the error that comes in case of using any else than boolean

package main

import "fmt"

func main() {
    if 1 {
        fmt.Println("a is greater than 5")
    }
}

Output: Below compiler error is raised

non-bool 1 (type int) used as if condition

Ternary Operator

There is no ternary operator in Go, hence you need to use if else statements in place of that.

Conclusion

That is all about if else statement in go. Hope you have liked this article. Please share feedback/improvements/mistakes in comments

Next Tutorial – Switch
Previous Tutorial – For Range loop

The post Understand If Else Statement in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/understand-if-else-statement-golang/feed/ 2 2088
Understanding time and date in Go (Golang) – Complete Guide https://vikasboss.github.io/all-about-time-and-date-golang/ https://vikasboss.github.io/all-about-time-and-date-golang/#comments Sat, 01 Feb 2020 08:08:05 +0000 https://vikasboss.github.io/?p=1292 Note: If you are interested in learning Golang, then for that we have a golang comprehensive tutorial series. Do check it out – Golang Comprehensive Tutorial Series. Now let’s see the current tutorial....

The post Understanding time and date in Go (Golang) – Complete Guide appeared first on Welcome To Golang By Example.

]]>
Note: If you are interested in learning Golang, then for that we have a golang comprehensive tutorial series. Do check it out – Golang Comprehensive Tutorial Series. Now let’s see the current tutorial. Below is the table of contents.

Overview

Time or Date is represented in Go using time.Time struct. time can be also be represented as a

  • Unix Time (Also known as Epoch Time) – It is the number of seconds elapsed since 00:00:00 UTC on 1 January 1970. This time is also known as the Unix epoch.

Structure

time.Time object is used to represent a specific point in time. The time.Time struct is as below

type Time struct {
    // wall and ext encode the wall time seconds, wall time nanoseconds,
    // and optional monotonic clock reading in nanoseconds.
    wall uint64
    ext  int64
    //Location to represent timeZone
    // The nil location means UTC
    loc *Location
}

As you can notice that every time.Time object has an associated location value which is used to determine the minute, hour, month, day and year corresponding to that time.

Create a new time

Using time.Now()

This function can be used to get the current local timestamp. The signature of the function is

func Now() Time

Using time.Date()

This function returns the time which is yyyy-mm-dd hh:mm:ss + nsec nanoseconds with the appropriate time zone corresponding to the given location. The signature of the function is:

func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time

Understanding Duration

duration is the time that has elapsed between two instants of time. It is represented as int64nanosecond count. So duration is nothing in Go but just a number representing time in nanoseconds. So if duration value is  equal to 1000000000 then it represents 1 sec or 1000 milliseconds or 10000000000 nanoseconds

As an example duration between two time values 1 hour apart will be below value which is equal number of nanoseconds in 1 hour.

1 *60*60*1000*1000*1000

It is represented as below in the time package.

type Duration int64

Below are some common duration which are defined in time package

const (
    Nanosecond  Duration = 1
    Microsecond          = 1000 * Nanosecond
    Millisecond          = 1000 * Microsecond
    Second               = 1000 * Millisecond
    Minute               = 60 * Second
    Hour                 = 60 * Minute
)

Some of the function defined on time.Time object that returns the Duration are

  • func (t Time) Sub(u Time) Duration – It returns the duration t-u
  • func Since(t Time) Duration – It returns the duration which has elapsed since t
  • func Until(t Time) Duration – It returns the duration until t

Add or Subtract to a time

Now that you have understood what duration , let’s see how we can add or subtract to a time instance

time package in golang defines two ways of adding or subtracting to a time.

  • Add function – It is used to add/subtract a duration to time t. Since duration can be represented in hours, minutes, seconds, milliseconds, microseconds and nanoseconds, therefore Add function can be used to add/subtract hours, minutes, seconds, milliseconds, microseconds and nanoseconds from a time . Its signature is
func (t Time) Add(d Duration) Time
  • AddDate function – It is used to add/subtract years, months and days to time t. Its signature is
func (t Time) AddDate(years int, months int, days int) Time

Note: Positive values are used to add to time and negative values are used to subtract. Let’s see a working example of Add and Subtract to time.

Add to time

Below code can be used to add to time

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()

    //Add 1 hours
    newT := t.Add(time.Hour * 1)
    fmt.Printf("Adding 1 hour\n: %s\n", newT)

    //Add 15 min
    newT = t.Add(time.Minute * 15)
    fmt.Printf("Adding 15 minute\n: %s\n", newT)

    //Add 10 sec
    newT = t.Add(time.Second * 10)
    fmt.Printf("Adding 10 sec\n: %s\n", newT)

    //Add 100 millisecond
    newT = t.Add(time.Millisecond * 10)
    fmt.Printf("Adding 100 millisecond\n: %s\n", newT)

    //Add 1000 microsecond
    newT = t.Add(time.Millisecond * 10)
    fmt.Printf("Adding 1000 microsecond\n: %s\n", newT)

    //Add 10000 nanosecond
    newT = t.Add(time.Nanosecond * 10000)
    fmt.Printf("Adding 1000 nanosecond\n: %s\n", newT)

    //Add 1 year 2 month 4 day
    newT = t.AddDate(1, 2, 4)
    fmt.Printf("Adding 1 year 2 month 4 day\n: %s\n", newT)
}

Output:

Adding 1 hour:
 2020-02-01 02:16:35.893847 +0530 IST m=+3600.000239893

Adding 15 minute:
 2020-02-01 01:31:35.893847 +0530 IST m=+900.000239893

Adding 10 sec:
 2020-02-01 01:16:45.893847 +0530 IST m=+10.000239893

Adding 100 millisecond:
 2020-02-01 01:16:35.903847 +0530 IST m=+0.010239893

Adding 1000 microsecond:
 2020-02-01 01:16:35.903847 +0530 IST m=+0.010239893

Adding 1000 nanosecond:
 2020-02-01 01:16:35.893857 +0530 IST m=+0.000249893

Adding 1 year 2 month 4 day:
 2021-04-05 01:16:35.893847 +0530 IST

Subtract to time

Below code can be used to subtract to time

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()

    //Add 1 hours
    newT := t.Add(-time.Hour * 1)
    fmt.Printf("Subtracting 1 hour:\n %s\n", newT)

    //Add 15 min
    newT = t.Add(-time.Minute * 15)
    fmt.Printf("Subtracting 15 minute:\n %s\n", newT)

    //Add 10 sec
    newT = t.Add(-time.Second * 10)
    fmt.Printf("Subtracting 10 sec:\n %s\n", newT)

    //Add 100 millisecond
    newT = t.Add(-time.Millisecond * 10)
    fmt.Printf("Subtracting 100 millisecond:\n %s\n", newT)

    //Add 1000 microsecond
    newT = t.Add(-time.Millisecond * 10)
    fmt.Printf("Subtracting 1000 microsecond:\n %s\n", newT)

    //Add 10000 nanosecond
    newT = t.Add(-time.Nanosecond * 10000)
    fmt.Printf("Subtracting 1000 nanosecond:\n %s\n", newT)

    //Add 1 year 2 month 4 day
    newT = t.AddDate(-1, -2, -4)
    fmt.Printf("Subtracting 1 year 2 month 4 day:\n %s\n", newT)
}

Output:

Subtracting 1 hour:
 2020-02-01 00:18:29.772673 +0530 IST m=-3599.999784391

Subtracting 15 minute:
 2020-02-01 01:03:29.772673 +0530 IST m=-899.999784391

Subtracting 10 sec:
 2020-02-01 01:18:19.772673 +0530 IST m=-9.999784391

Subtracting 100 millisecond:
 2020-02-01 01:18:29.762673 +0530 IST m=-0.009784391

Subtracting 1000 microsecond:
 2020-02-01 01:18:29.762673 +0530 IST m=-0.009784391

Subtracting 1000 nanosecond:
 2020-02-01 01:18:29.772663 +0530 IST m=+0.000205609

Subtracting 1 year 2 month 4 day:
 2018-11-27 01:18:29.772673 +0530 IST

Time Parsing/Formatting

If you have worked with time/date formatting/parsing in other languages you might have noticed that the other languages use special placeholders for time/date formatting. For eg ruby language uses

  • %d for day
  • %Y for year

etc

Golang, instead of using codes such as above, uses date and time format placeholders that look like date and time only. Go uses standard time, which is:

Mon Jan 2 15:04:05 MST 2006  (MST is GMT-0700)
or 
01/02 03:04:05PM '06 -0700


So if you notice go uses

  • 01 for day of the month ,
  • 02 for the month
  • 03 for hours ,
  • 04 for minutes
  • 05 for second
  • and so on

Below placeholder table describes the exact mapping. Go takes a more pragmatic approach where you don’t need to remember or lookup for the traditional formatting codes as in other languages

TypePlaceholder
Day2 or 02 or _2
Day of WeekMonday or Mon
Month01 or 1 or Jan or January
Year2006 or 06
Hour03 or 3 or 15
Minutes04 or 4
Seconds05 or 5
Milli Seconds  (ms).000        //Trailing zero will be includedor .999   //Trailing zero will be omitted
Micro Seconds (μs).000000             //Trailing zero will be includedor .999999        //Trailing zero will be omitted
Nano Seconds (ns).000000000        //Trailing zero will be includedor .999999999 //Trailing zero will be omitted
am/pmPM or pm
TimezoneMST
Timezone offset Z0700 or Z070000 or Z07 or Z07:00 or Z07:00:00  or -0700 or  -070000 or -07 or -07:00 or -07:00:00

Time Parse Example

Now coming back to time.Parse. The signature of the function is

func Parse(layout, value string) (Time, error)

time.Parse function takes in two arguments

  • First argument is the layout consisting of time format placeholder
  • Second argument is the actual formatted string representing a time.

The way you have to go about this is to make sure that the layout string (first argument ) matches the string representation (second argument) of the time you want to parse into time.Time. For parsing

  • For parsing 2020-01-29, layout string should be 06-01-02 or 2006-01-02 or something which maps correctly based on above placeholder table.
  • Similarly for parsing “2020-Jan-29 Wednesday 12:19:25” the layout string can be “2006-Jan-02 Monday 03:04:05”

Below are the working Code Examples of time.Parse().

package main

import (
    "fmt"
    "time"
)

func main() {
    //Parse YYYY-MM-DD
    timeT, _ := time.Parse("2006-01-02", "2020-01-29")
    fmt.Println(timeT)

    //Parse YY-MM-DD
    timeT, _ = time.Parse("06-01-02", "20-01-29")
    fmt.Println(timeT)

    //Parse YYYY-#{MonthName}-DD
    timeT, _ = time.Parse("2006-Jan-02", "2020-Jan-29")
    fmt.Println(timeT)

    //Parse YYYY-#{MonthName}-DD WeekDay HH:MM:SS
    timeT, _ = time.Parse("2006-Jan-02 Monday 03:04:05", "2020-Jan-29 Wednesday 12:19:25")
    fmt.Println(timeT)

    //Parse YYYY-#{MonthName}-DD WeekDay HH:MM:SS PM Timezone TimezoneOffset
    timeT, _ = time.Parse("2006-Jan-02 Monday 03:04:05 PM MST -07:00", "2020-Jan-29 Wednesday 12:19:25 AM IST +05:30")
    fmt.Println(timeT)
}

Output:

2020-01-29 00:00:00 +0000 UTC
2020-01-29 00:00:00 +0000 UTC
2020-01-29 00:00:00 +0000 UTC
2020-01-29 12:19:25 +0000 UTC
2020-01-29 00:19:25 +0530 IST

Time Formatting Example

time.Format function can be used to format time to a string representation. The signature of the function is

func (t Time) Format(layout string)

Let’s see some time format code examples

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    
    //Format YYYY-MM-DD
    fmt.Printf("YYYY-MM-DD: %s\n", now.Format("2006-01-02"))

    //Format YY-MM-DD
    fmt.Printf("YY-MM-DD: %s\n", now.Format("06-01-02"))

    //Format YYYY-#{MonthName}-DD
    fmt.Printf("YYYY-#{MonthName}-DD: %s\n", now.Format("2006-Jan-02"))

    //Format HH:MM:SS
    fmt.Printf("HH:MM:SS: %s\n", now.Format("03:04:05"))

    //Format HH:MM:SS Millisecond
    fmt.Printf("HH:MM:SS Millisecond: %s\n", now.Format("03:04:05 .999"))

    //Format YYYY-#{MonthName}-DD WeekDay HH:MM:SS
    fmt.Printf("YYYY-#{MonthName}-DD WeekDay HH:MM:SS: %s\n", now.Format("2006-Jan-02 Monday 03:04:05"))

    //Format YYYY-#{MonthName}-DD WeekDay HH:MM:SS PM Timezone TimezoneOffset
    fmt.Printf("YYYY-#{MonthName}-DD WeekDay HH:MM:SS PM Timezone TimezoneOffset: %s\n", now.Format("2006-Jan-02 Monday 03:04:05 PM MST -07:00"))
}

Output:

YYYY-MM-DD: 2020-01-25
YY-MM-DD: 20-01-25
YYYY-#{MonthName}-DD: 2020-Jan-25
HH:MM:SS: 11:14:16
HH:MM:SS Millisecond: 11:14:16 .213
YYYY-#{MonthName}-DD WeekDay HH:MM:SS: 2020-Jan-25 Saturday 11:14:16
YYYY-#{MonthName}-DD WeekDay HH:MM:SS PM Timezone TimezoneOffset: 2020-Jan-25 Saturday 11:14:16 PM IST +05:30

Time Diff

time package has a method Sub which can be used to get the difference between two different time values. The signature of the function is

func (t Time) Sub(u Time) Duration
currentTime := time.Now()
oldTime := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
diff := currentTime.Sub(oldTime)

Time Conversion

Below code shows conversion of

  • time.Time to Unix Timestamp
  • Unix Timestamp to time.Time
package main

import (
    "fmt"
    "time"
)

func main() {
    tNow := time.Now()

    //time.Time to Unix Timestamp
    tUnix := tNow.Unix()
    fmt.Printf("timeUnix %d\n", tUnix)

    //Unix Timestamp to time.Time
    timeT := time.Unix(tUnix, 0)
    fmt.Printf("time.Time: %s\n", timeT)
}

Output:

timeUnix 1257894000
time.Time: 2009-11-10 23:00:00 +0000 UTC

Convert time between different timezones

The In function can be used to change the location associated with a particular time.Time object. Whenever the In function is called on any time.Time object (say t)  then,

  • A copy of t is created representing the same time instant.
  • t’s location is set to the location passed to In function for display purposes
  • t is returned back

Let’s see the below working code which can be used to change the location value associated with a particular time.

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()

    loc, _ := time.LoadLocation("UTC")
    fmt.Printf("UTC Time: %s\n", now.In(loc))
   
    loc, _ = time.LoadLocation("Europe/Berlin")
    fmt.Printf("Berlin Time: %s\n", now.In(loc))

    loc, _ = time.LoadLocation("America/New_York")
    fmt.Printf("New York Time: %s\n", now.In(loc))

    loc, _ = time.LoadLocation("Asia/Dubai")
    fmt.Printf("Dubai Time: %s\n", now.In(loc))
}

Output:

UTC Time: 2020-01-31 18:09:41.705858 +0000 UTC
Berlin Time: 2020-01-31 19:09:41.705858 +0100 CET
New York Time: 2020-01-31 13:09:41.705858 -0500 EST
Dubai Time: 2020-01-31 22:09:41.705858 +0400 +04

The post Understanding time and date in Go (Golang) – Complete Guide appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/all-about-time-and-date-golang/feed/ 1 1292