How to resolve the algorithm Last Friday of each month step by step in the Go programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Last Friday of each month step by step in the Go programming language
Table of Contents
Problem Statement
Write a program or a script that returns the date of the last Fridays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).
Example of an expected output:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Last Friday of each month step by step in the Go programming language
Package and Imports:
- The program starts by defining a package named "main" and importing necessary packages:
- "fmt": For formatting and printing output.
- "os": For interacting with the operating system ( specifically to retrieve command-line arguments).
- "strconv": For converting strings to integers.
- "time": For date and time manipulation.
Main Function:
- The
main
function is the entry point of the program: - It retrieves the current year using
time.Now().Year()
. - If there is a command-line argument provided, it attempts to convert it to an integer using
strconv.Atoi
, and if successful, it sets the year to the provided value.
Loop to Print First Fridays of Each Month:
- The program iterates through all months in the year (from January to December):
- For each month, it subtracts 24 hours to get the end of the previous month.
- It calculates the first Friday of the month by subtracting the weekday (converting it to an integer and adding 7 to ensure it's in the range 0-6) multiplied by 24 hours.
- The calculated date is formatted and printed using
d.Format("2006-01-02")
.
Output: The program's output consists of a list of first Fridays for each month in the specified year, in the format "YYYY-MM-DD".
Example:
If you run the program with the command go run main.go 2023
, the output would be:
2023-01-06
2023-02-03
2023-03-03
2023-04-07
2023-05-05
2023-06-02
2023-07-07
2023-08-04
2023-09-01
2023-10-06
2023-11-03
2023-12-01
Source code in the go programming language
package main
import (
"fmt"
"os"
"strconv"
"time"
)
func main() {
y := time.Now().Year()
if len(os.Args) == 2 {
if i, err := strconv.Atoi(os.Args[1]); err == nil {
y = i
}
}
for m := time.January; m <= time.December; m++ {
d := time.Date(y, m+1, 1, 0, 0, 0, 0, time.UTC).Add(-24 * time.Hour)
d = d.Add(-time.Duration((d.Weekday()+7-time.Friday)%7) * 24 * time.Hour)
fmt.Println(d.Format("2006-01-02"))
}
}
You may also check:How to resolve the algorithm Exponentiation operator step by step in the C# programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Delphi programming language
You may also check:How to resolve the algorithm Tau function step by step in the Action! programming language
You may also check:How to resolve the algorithm Tau function step by step in the Rust programming language