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

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

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,777 questions

55,543 answers

573 users

How to create an array of dates 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 dates
	dates := make([]time.Time, 30)

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

	for _, dt := range dates {
		fmt.Println(dt.Format("2006-01-02"))
	}
}


/*
run:

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

*/

 



answered Apr 11, 2025 by avibootz
...