[SOLVED] How to install Go libraries not in the standard library?

Let’s call this program “playing_with_imports.go”.

package main

import (
  "fmt"
  // "mat"
)

func main() {
  fmt.Println("Hi!")
}

I can run it, via go run playing_with_imports.go. However, if I uncomment the line containing the word “mat”, thus trying to import the “mat” library, it won’t build. It gives me this error:

playing_with_imports.go:4:10: package mat is not in GOROOT (/nix/store/z3cc4kbqpg8yyy1ak9xzp3b5mi0ydvs9-go-1.17.3/share/go/src/mat)

The problem seems clear: Go libraries are installed in the nix store, so I can’t procedurally download them via whatever mechanism non-NixOS Go programmers use.

How, then, should I download them?

Unless you are in a derivation context, it works just fine, though as mat is not a “URL-like” package name (no . and / in it) go assumes that it is a stdlib package and looks it up through GOROOTonly.

1 Like

You’re completely right. Not a NixOS problem. I just had to give the fully qualified name of the library, Thanks, @Nobbz!

Hosted by Flying Circus.