10
submitted 11 months ago by lyda@programming.dev to c/golang@programming.dev

Say I have go:generate directives in two files: one in the foo/ directory and one in the mock/ directory. The generated mock code will need data structures from the generated code in the foo module. Will go generate ./... reliably generate code in the correct order? What do I need to do to make that happen?

all 3 comments
sorted by: hot top controversial new old
[-] atheken@programming.dev 2 points 11 months ago* (last edited 11 months ago)

I'm a go n00b, but since the source code is available, I figured I'd look. TL;DR: it probably uses the shell's default globbing resolution to produce the file list.

The generate command iterates over the internal files, but I can't find exactly how GoFiles is populated.

You can probably learn what it's doing by running go generate -n or go generate -x, and I think you can also explicitly call go generate with a file pattern list, which would give you this control.

Otherwise, I think you can include more than one magic comment in a single file, so if you have some dependant generators, these could be placed in the same file, sequentially, and you'd get the expected result.

Another alternative would be to try renaming the relevant files so that they sort the way you want them to run, lexicographically.

[-] lyda@programming.dev 2 points 11 months ago

Good answers. I like these. I like the more than one command in a file, that will work. And yes, should have read the source!

this post was submitted on 02 Oct 2023
10 points (100.0% liked)

Golang

1707 readers
1 users here now

This is a community dedicated to the go programming language.

Useful Links:

Rules:

founded 1 year ago
MODERATORS