Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,140 questions

56,014 answers

573 users

How to create an array of days starting with today and going back the last 30 days in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "time"
)

func main() {
    // Get today's date
    today := time.Now()

    // Create a slice to hold the days
    days := make([]time.Time, 30)

    // Populate the slice with dates from today going back 30 days
    for i := 0; i < 30; i++ {
        days[i] = today.AddDate(0, 0, -i)
    }

    for _, day := range days {
        fmt.Println(day.Format("02")) // Extract and print only the day of the month
    }
}



/*
run:

11
10
09
08
07
06
05
04
03
02
01
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13

*/

 



answered Apr 11, 2025 by avibootz
...