GPT-4 (and maybe more) really is amazing...sometimes

Just because you can do something is enough reason to do it Posted by Art Mills

Apr 12, 2023

When it works, it works

Coming mostly from a Ruby and Spring background, you tend to seek out ways to make your project go simply by searching for the package someone else already wrote that everyone else is already using, snap it in, and it works, and you're done, and it's time for the drinking.

With that mindset there can be answers within the Golang community. But one of the biggest selling points of the language is it's pretty much designed to be self contained enough to do pretty much everything. Still, when I wanted to add a sitemap to this site I did the thing I've always done.

Search for the package someone else wrote that everyone else is using.

NONE EXISTS

I mean, yes, there are packages to generate sitemaps. Though either they are a few months old that no one is using, or several years old that very few used. I asked ChatGPT for advice as to which package might work best.

While the program can be infuriating at times, there are times it's quite magical.

This was one of those times

GPT-4, in just before the rate limit hit, tells me to use neither. If I just want a sitemap, just do this...and it lists out a complete sitemap addition to this site. I didn't like how it did it and suggested it shouldn't go smack in the middle of my main.go file, and it should be a separate package, and what about my notes area where that will constantly be added as I write here.

And it came up with this:



package sitemap

import (
	"encoding/xml"
	"time"
)

type URLSet struct {
	XMLName xml.Name `xml:"urlset"`
	Xmlns   string   `xml:"xmlns,attr"`
	URLs    []URL    `xml:"url"`
}

type URL struct {
	Loc        string    `xml:"loc"`
	LastMod    time.Time `xml:"lastmod"`
	ChangeFreq string    `xml:"changefreq"`
	Priority   float64   `xml:"priority"`
}

func NewURLSet(urls []URL) *URLSet {
	return &URLSet{
		Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
		URLs:  urls,
	}
}

func (urlSet *URLSet) ToXML() ([]byte, error) {
	xmlBytes, err := xml.MarshalIndent(urlSet, "", "  ")
	if err != nil {
		return nil, err
	}
	return []byte(xml.Header + string(xmlBytes)), nil
}

Then it made the following main.go changes:



	// Sitemap route
	r.GET("/sitemap.xml", func(c *gin.Context) {
		// Retrieve all notes from the database
		notes, err := models.NotesAll()
		if err != nil {
			c.AbortWithStatus(http.StatusInternalServerError)
			return
		}

		urls := []sitemap.URL{
			{Loc: "https://hamcois.com/", LastMod: time.Now(), ChangeFreq: "daily", Priority: 1.0},
			{Loc: "https://hamcois.com/notes", LastMod: time.Now(), ChangeFreq: "daily", Priority: 0.9},
			{Loc: "https://hamcois.com/about", LastMod: time.Now(), ChangeFreq: "daily", Priority: 0.9},

			// Add more static URLs as needed
		}

		// Generate URLs for individual notes
		for _, note := range notes {
			urls = append(urls, sitemap.URL{
				Loc:        fmt.Sprintf("https://hamcois.com/notes/%d", note.ID),
				LastMod:    note.UpdatedAt,
				ChangeFreq: "weekly",
				Priority:   0.8,
			})
		}

		urlSet := sitemap.NewURLSet(urls)
		xmlBytes, err := urlSet.ToXML()
		if err != nil {
			c.AbortWithStatus(http.StatusInternalServerError)
			return
		}
		c.Data(http.StatusOK, "application/xml", xmlBytes)
	})

I didn't write a single line here.

I snapped it in and it works. Go to my sitemap here .

Is it the most advanced thing ever?

No. But it works. Instantly.

It took seconds to achieve. And I had no clue as to even how to begin to approach "making my own". The do it yourself aspect of Go is somewhat daunting. But also compelling.

My oldest daughter can't type. Because high school sucks. She was never taught. I need to teach her how to type and my first thought was to find a program and buy it. But now I'm going to see if ChatGPT can lead me along the path of making a tiny typing program.

One I will even write stuff for.

Pretty sure I'll end up just buying a program. But for today I am inspired.