Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Get age given a DOB in Go (Golang)

Posted on January 23, 2023January 24, 2023 by admin

This tutorial will talk about how given a date of birth we can compute the age of a person. go-age comes to our rescue for doing that. It also takes into account the complexities of leap year while calculating age.

See a below working example:

package main
import (
"fmt"
"time"
age "github.com/bearbin/go-age"
)
func main() {
dob := getDOB(2011, 4, 2)
fmt.Printf("Age is %d\n", age.Age(dob))
}
func getDOB(year, month, day int) time.Time {
dob := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
return dob
}

Output:

Age is 8
  • age
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP