Go mod’s lesser known features
verdverm.com
verdverm.com
I wish Go had got these names the right way round. Every other programming language and packaging (NB. not "modeling"!) ecosystem uses package to mean distribution, and module to mean compilation unit.
Then again, maybe we should make up new terms instead of re-using existing ones. Like Ruby “Gems” or Homebrew’s “Casks”. It’s not intrinsically descriptive, but it is unique and unambiguous. I suppose again it could be worse, we could be talking about “libraries” and “frameworks” while describing “modules” and “packages”.
I never remember having seen them referenced as modules.
Yes modules in Java don't mean the same thing as in Modula-2, Ada has packages, child packages and nested packages, Object Pascal/UCSD Pascal has units, while VMS Pascal has modules, and so on.
There is plenty of literature to educate onself about the subject instead of Internet random searches.
Package = Lego bricks (probably share a theme and are meant to build something concrete together), shipped with instructions and media and whatnot, in a, well, whole package.
the lack of “one true way” is very strange. some projects module. some projects check in dependent source code. some projects have a version number in the package name. do i download with go get? or go install? or go get -u?
i can’t think of another build system that has so much variation just in declaring and importing dependencies
> most of the documentation talks of gopath
Historically go used a GOPATH env var. Go code had to live under there under `$GOPATH/src/<module-path>`. When doing an import, go would look at the import path, and find it in GOPATH. This wasn't flexible enough but still exists under the hood as it's where go saves (caches?) packages globally. If you have a go.mod/go.sum file in your project, in general, you won't have to worry about GOPATH.
Worth noting that go, while widely used, was created to solve Google's problems (I guess at least initially?), where (from my understanding) they have a huge monorepo. At that point, having everything live under such a GOPATH somewhat makes sense.
> why are go mod vendor and go mod tidy different commands?
Go mod vendor puts all the dependencies in a ./vendor directory. Often used to commit your packages directly into git. There might be other advantages, not sure, but personally I don't use it. Go is able to pick up the packages in your go.mod directly from $GOPATH.
Go mod tidy ensures your go.mod matches your used package. E.g. if you remove usage of a package in your code, go mod tidy is able to pick that up. In addition, it also ensures your go.sum matches go.mod.
> the lack of “one true way” is very strange. some projects module. some projects check in dependent source code
This is a historic issue. Prior to 1.12 (I think), go modules didn't exist, and there were different community projects that attempted to solve go package management.
> do i download with go get? or go install? or go get -u?
Again, historic issues, iirc go get -u forces an update of a package, meaning this updates your go.mod file. Go get without -u does not force update the package. It update your go.mod file if you haven't included the module previously. Go install is used for go main packages. It fetches the code, builds it (so it must be a main package), and puts the final binary in $GOPATH/bin. Assuming you have that in your patch, you can use it straight away.
Actually writing the code was fun at first, but everything else around the actual act of composing code was thoroughly awful UX.
Now this gives an error "go.mod file not found in current directory", but if I add that file, go build says "unexpected go.mod file found in current directory".
Luckily, for now, I can use GO111MODULE=off to keep pretending everything is fine, but my eyes start to glaze over when I read the migration guide at https://go.dev/blog/migrating-to-go-modules - it's one of those moments where I start to wish I'd used a different language :(
1. Go mod init in your code’s root dir 2. Done..? Go get ./…?
"go mod init" returns the error "go: cannot determine module path for source directory".
"go mod init src/mything/mypackage" creates a go.mod file, but then "env GOPATH=$PWD go build mything/mypackage" says "$GOPATH/go.mod exists but should not" (as opposed to when it isn't there, then go complains "go.mod file not found in current directory")
Note that "env GOPATH=$PWD GO111MODULE=off go build mything/mypackage" does build all the source code.
Most likely I've set the whole project and directory structure up wrong, and it only works by brute force.
Just use go get to get initially, and go mod to organize thereafter. It is very simple and occupies <1% of my cognitive effort writing go code.
Imho, many languages would massively benefit with the kind of tooling Go has out of the box. I heard Rust's tooling has some great stuff too and it's in my immediate plan to learn some Rust.
I forget the solution, but involved messing about with the `GOPRIVATE` env variable, adding some `.insteadOf` entries to git config, plus similar fun trying to get the same happening in a CI environment.
This is only true if you want the module to be publicly 'go get'-able. Private modules can be named whatever you want.
(Some tools use whether or not the first import path segment contains a '.' as a heuristic for "is this package stdlib", and those won't work correctly on a module that doesn't use a dot. There's a proposal, not yet accepted, to document this as a naming requirement for modules: https://github.com/golang/go/issues/32819 This is of course a looser requirement than "must be a domain name".)
but also for security resaons. << reasons not resaons