Adventures in Go Mod

I was trying to write a quick sketch for a presentation at work, and wanted to use one of my repos. It's hosted on my gitea site, though, and corporate blocks that. Fair enough, I have a mirror on Github.

package main

import (
    "crypto/rand"
    "crypto/rsa"
    "fmt"

    "git.wntrmute.dev/kyle/goutils/die"
)

func main() {
    priv, err := rsa.GenerateKey(rand.Reader, 2048)
    die.If(err)

    fmt.Printf("p=%s\n", priv.Primes[0])
    fmt.Printf("q=%s\n", priv.Primes[1])
    fmt.Printf("e=%d\n", priv.E)
    fmt.Printf("n=%s\n", priv.N)
}

But... I kept running into problems. First, I tried importing the Github version: github.com/kisom/goutils/die. That gave me a problem:

go: downloading github.com/kisom/goutils v1.7.7
go: github.com/kisom/goutils@v1.7.7: parsing go.mod:
    module declares its path as: git.wntrmute.dev/kyle/goutils
            but was required as: github.com/kisom/goutils

Jeremy and Aaron gave me advice: replace the module in go.mod. I didn't know anything about that, so I pulled up the go docs on the subject. A little confusing, but this should work - right?

module demo

go 1.22.2

replace (
    git.wntrmute.dev/kyle/goutils v1.7.7 => github.com/kisom/goutils v1.7.7
    git.wntrmute.dev/kyle/goutils v1.7.7 => github.com/kisom/goutils v1.7.7
)

require (
    git.wntrmute.dev/kyle/goutils v1.7.7
)

It took a few tries to figure out "pkg version" vs "pkg@version" (consistency is one of the hard parts of comp sci, right?) but fine, go mod accepts this.

Still the same problem.

The answer is: do the replacement in go.mod, and import it as you normally would:

package main

import (
    "crypto/rand"
    "crypto/rsa"
    "fmt"

    "git.wntrmute.dev/kyle/goutils/die"
)

func main() {
    priv, err := rsa.GenerateKey(rand.Reader, 2048)
    die.If(err)

    fmt.Printf("p=%s\n", priv.Primes[0])
    fmt.Printf("q=%s\n", priv.Primes[1])
    fmt.Printf("e=%d\n", priv.E)
    fmt.Printf("n=%s\n", priv.N)
}

That works, and here we go:

(base) kyle@titan demo % go get
(base) kyle@titan demo % go run rsa.go
p=17239756214575124383420380590150129192251685278677181600343946
2387590945090216725631672410840447862167965232849015126866289644
4927592095615822732595141927790133814875101328027585980160595751
3583466685364256157291486044164854017824654964207385204596115309
9919656784796630686214873964370638620500417247975309621
q=15144230501372153729144986514624535591559780415273478501286177
6917731179436200759921653393062801313337655810465966258278243189
4712222260689975217912960648679760529204891137433090862802874233
8560155167882847754004411902777000376713663347067260651352168253
5725307890702884810324059954100137186724268450968889703
e=65537
n=261082841900988739008068335294836611714896652706749363937316200
18718459039123562271804561501862705722720452921100177778592851228
44919242469341188710786507809679291005227659083222918503408440729
37062159383440668278379978536506996141151193663353841488467245675
07306015181735919894700955490265104052737256239456677386857048742
02679245878296481155790929820391135674264776232486812990724257382
70245321614243209327007824047622341422458888479173620171200150737
57451728208257282933924999918931166904281144329659371215660875716
46637424453866676885223842897978777887472382942561231457476922265
6605196406988622238632540023732563

Tags: , ,