Deadcode: Finding unreachable functions in Go
go.dev
go.dev
[1]: https://www.micahcantor.com/blog/js-of-ocaml-dead-code/
golangci-lint rolls up lot of linters and checkers into a single binary.
There is a config file too.
https://go.dev/play/p/hg-ugl7i0ei demonstrates this, I think?
go vet -basilisk ./...I would do the first-time compile on this template and then add things iteratively.
Go would be a nonstarter for this experimental/artistic code, because I would usually have dead code in my "toolbox" to iterate and experiment with until I liked the end product.
My reality is that I don't comment and uncomment lines, and I don't want to start. Commenting out a block of code takes more time and it's still unused code.
It really feels like a "Go is designed around problems Google had within Google" design choice.
if 1==2 { ...some code to temporarily disable ... }
"Just comment it out" is not always enough because when I want to quickly toggle the code on and off multiple times in a single session, it's not possible to do by only commenting/uncommenting that single block.Sometimes the code that's commented out contains the only reference to an imported package, and by commenting it out the autoformatter helpfully removes the import from the top of the file, so then uncommenting the line is not enough -- I need to go back and re-add the import.
Or I may want to comment out this line
x = f(a, b)
but that makes a and b in turn become unused as well, and I need to travel up to wherever they're defined and comment them out too, and so on with anything else that became thus unused in that chain.And this is all suboptimal because the warning is also important when debugging. I do want to get warned about the unused code! Just let me compile.
The same tool that removes your imports also adds them so there usually isn’t an extra step.
``` import "alpha/stuff" import bstuff "beta/stuff"
stuff.buyStuff() bstuff.stuffAnimal() ```
Once the autoformatter sees the named import is unused it removes it. Then when I uncomment my code again the autoformatter doesn't remember that it's supposed to use a named import. So in my example it wouldn't know "bstuff" is actually "beta/stuff".
Does anyone know how to work around this?
deadcode ./cmd/...TL;DR: if your project is a library and an exported function looks like dead code, that means it isn't covered by any tests, and you should probably fix that.
Most have it on the standard tools, but I don't think any language was born with it.
Doesn't seem far-fetched to ask for the compiler to warn on unused types/functions too.
So there are two choices here:
1. The Go compiler complains about code which is unused by the current main package.
2. The Go compiler tries to find/compile all main packages every-time.
Both of these options suck.Tree shaking shouldn't be necessary in 2024 anyway.
Even budget phones routinely have more than 4GB of RAM, and high-speed Internet access is the norm, so you can get away with a lot more inefficiency than just a few years ago.
Is this not the Greeter interface example from the OP? The example finds an unused variant of an interface and all code only invoked by said unused implementations and marks all of it as deadcode.